Search in sources :

Example 1 with ObjectSchema

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);
            }
        }
    }
}
Also used : Reference(org.sagebionetworks.repo.model.Reference) Field(java.lang.reflect.Field) ObjectSchema(org.sagebionetworks.schema.ObjectSchema) JSONEntity(org.sagebionetworks.schema.adapter.JSONEntity) HashSet(java.util.HashSet)

Example 2 with ObjectSchema

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();
    }
}
Also used : ObjectSchema(org.sagebionetworks.schema.ObjectSchema) UserProfile(org.sagebionetworks.repo.model.UserProfile) UserGroup(org.sagebionetworks.repo.model.UserGroup)

Example 3 with ObjectSchema

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);
}
Also used : ObjectSchema(org.sagebionetworks.schema.ObjectSchema) UserProfile(org.sagebionetworks.repo.model.UserProfile) UnauthorizedException(org.sagebionetworks.repo.model.UnauthorizedException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with ObjectSchema

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;
}
Also used : ObjectSchema(org.sagebionetworks.schema.ObjectSchema) UserProfile(org.sagebionetworks.repo.model.UserProfile) QueryResults(org.sagebionetworks.repo.model.QueryResults) UserGroup(org.sagebionetworks.repo.model.UserGroup)

Example 5 with ObjectSchema

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;
}
Also used : ObjectSchema(org.sagebionetworks.schema.ObjectSchema) LinkDescription(org.sagebionetworks.schema.LinkDescription)

Aggregations

ObjectSchema (org.sagebionetworks.schema.ObjectSchema)73 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)15 UserProfile (org.sagebionetworks.repo.model.UserProfile)12 JSONObjectAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)11 JBlock (com.sun.codemodel.JBlock)9 JMethod (com.sun.codemodel.JMethod)9 List (java.util.List)9 UserGroup (org.sagebionetworks.repo.model.UserGroup)9 JVar (com.sun.codemodel.JVar)8 Annotations (org.sagebionetworks.repo.model.Annotations)8 NamedAnnotations (org.sagebionetworks.repo.model.NamedAnnotations)8 JDefinedClass (com.sun.codemodel.JDefinedClass)7 Field (java.lang.reflect.Field)7 JFieldVar (com.sun.codemodel.JFieldVar)6 RenameFieldData (org.sagebionetworks.repo.model.registry.EntityMigrationData.RenameFieldData)6 JDocComment (com.sun.codemodel.JDocComment)5 JExpression (com.sun.codemodel.JExpression)5 JFieldRef (com.sun.codemodel.JFieldRef)5 JInvocation (com.sun.codemodel.JInvocation)5