use of org.sagebionetworks.schema.ObjectSchema in project Synapse-Repository-Services by Sage-Bionetworks.
the class NodeTranslationUtils method updateNodeSecondaryFieldsFromObject.
/**
* Add any fields from the object that are not on a node.
*
* @param <T>
* @param base
* @param annos
* @param references
* @throws IllegalArgumentException
*/
public static <T extends Entity> void updateNodeSecondaryFieldsFromObject(T base, Annotations annos, Map<String, Set<Reference>> references) {
if (base == null)
throw new IllegalArgumentException("Base cannot be null");
if (annos == null)
throw new IllegalArgumentException("Annotations cannot be null");
// Find the fields that are not on nodes.
ObjectSchema schema = SchemaCache.getSchema(base);
Map<String, ObjectSchema> schemaProperties = schema.getProperties();
if (schemaProperties == null) {
schemaProperties = new HashMap<String, ObjectSchema>();
}
Field[] fields = base.getClass().getDeclaredFields();
for (Field field : fields) {
String name = field.getName();
String nodeName = nameConvertion.get(name);
if (nodeName == null) {
nodeName = name;
}
// Is this a field already on Node?
if (!nodeFieldNames.containsKey(nodeName)) {
// Make sure we can call it.
field.setAccessible(true);
Object value;
try {
value = field.get(base);
// Skip any property not defined in the schema
ObjectSchema propSchema = schemaProperties.get(name);
if (propSchema == null) {
continue;
}
// If this is an enum then store the string
if (propSchema.getEnum() != null) {
value = NodeTranslationUtils.getNameFromEnum(value);
}
// skip any transient property as they are not stored.
if (propSchema.isTransient())
continue;
// First off is this a collection?
if (propSchema.getItems() != null) {
// Is this a reference
if (Reference.class.getName().equals(propSchema.getItems().getId())) {
if (value == null) {
references.remove(name);
} else {
references.put(name, (Set<Reference>) value);
}
continue;
}
}
// Is this a single references?
if (propSchema.getId() != null) {
if (Reference.class.getName().equals(propSchema.getId())) {
HashSet<Reference> set = new HashSet<Reference>();
if (value == null) {
references.remove(name);
} else {
set.add((Reference) value);
references.put(name, set);
continue;
}
}
}
// The schema type will tell us how to store this
if (value == null) {
annos.deleteAnnotation(name);
} else {
if (propSchema.getContentEncoding() != null || value instanceof JSONEntity) {
// This will be stored a a blob
byte[] blob = objectToBytes(value, propSchema);
annos.replaceAnnotation(name, blob);
} else {
annos.replaceAnnotation(name, value);
}
}
} catch (IllegalAccessException e) {
// This should never occur
log.log(Level.WARNING, e.getMessage(), e);
}
}
}
}
use of org.sagebionetworks.schema.ObjectSchema in project Synapse-Repository-Services by Sage-Bionetworks.
the class UserManagerImpl method getDisplayName.
/**
* @param principalId
* @return for a group, returns the group name, for a user returns the display name in the user's profile
*/
@Override
public String getDisplayName(Long principalId) throws NotFoundException, DatastoreException {
UserGroup userGroup = userGroupDAO.get(principalId.toString());
if (userGroup.getIsIndividual()) {
ObjectSchema schema = SchemaCache.getSchema(UserProfile.class);
UserProfile userProfile = userProfileDAO.get(principalId.toString(), schema);
return userProfile.getDisplayName();
} else {
return userGroup.getName();
}
}
use of org.sagebionetworks.schema.ObjectSchema in project Synapse-Repository-Services by Sage-Bionetworks.
the class UserProfileManagerImpl method updateUserProfile.
/**
* This method is only available to the object owner or an admin
*/
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
@Override
public UserProfile updateUserProfile(UserInfo userInfo, UserProfile updated) throws NotFoundException, DatastoreException, UnauthorizedException, ConflictingUpdateException, InvalidModelException {
ObjectSchema schema = SchemaCache.getSchema(UserProfile.class);
UserProfile userProfile = userProfileDAO.get(updated.getOwnerId(), schema);
boolean canUpdate = UserProfileManagerUtils.isOwnerOrAdmin(userInfo, userProfile.getOwnerId());
if (!canUpdate)
throw new UnauthorizedException("Only owner or administrator may update UserProfile.");
attachmentManager.checkAttachmentsForPreviews(updated);
return userProfileDAO.update(updated, schema);
}
use of org.sagebionetworks.schema.ObjectSchema in project Synapse-Repository-Services by Sage-Bionetworks.
the class UserProfileManagerImpl method getInRange.
@Override
public QueryResults<UserProfile> getInRange(UserInfo userInfo, long startIncl, long endExcl, boolean includeEmail) throws DatastoreException, NotFoundException {
ObjectSchema schema = SchemaCache.getSchema(UserProfile.class);
List<UserProfile> userProfiles = userProfileDAO.getInRange(startIncl, endExcl, schema);
long totalNumberOfResults = userProfileDAO.getCount();
for (UserProfile userProfile : userProfiles) {
UserProfileManagerUtils.clearPrivateFields(userProfile);
if (includeEmail) {
UserGroup userGroup = userGroupDAO.get(userProfile.getOwnerId());
if (userGroup != null)
userProfile.setEmail(StringUtil.obfuscateEmailAddress(userGroup.getName()));
}
}
QueryResults<UserProfile> result = new QueryResults<UserProfile>(userProfiles, (int) totalNumberOfResults);
return result;
}
use of org.sagebionetworks.schema.ObjectSchema in project Synapse-Repository-Services by Sage-Bionetworks.
the class UserProfileManagerUtils method isPublic.
/**
* Determines from the UserProfile schema whether a field is public
* @param property
* @return
*/
public static boolean isPublic(String property) {
// profile picture is always public, even though AttachmentData might not be
if (property.equals("pic"))
return true;
ObjectSchema schema = SchemaCache.getSchema(UserProfile.class);
Map<String, ObjectSchema> schemaProperties = schema.getProperties();
ObjectSchema propertySchema = schemaProperties.get(property);
if (propertySchema == null)
throw new RuntimeException("No property " + property + " found in UserProfile schema.");
LinkDescription[] linkDescriptions = propertySchema.getLinks();
// by default a property is not public
if (linkDescriptions == null || linkDescriptions.length == 0)
return false;
for (LinkDescription ld : linkDescriptions) {
if (ld.getRel().equals(LinkRel.DESCRIBED_BY) && PUBLIC_PROPERTY_LINK.equals(ld.getHref()))
return true;
}
return false;
}
Aggregations