Search in sources :

Example 1 with EntityPath

use of org.sagebionetworks.repo.model.EntityPath in project Synapse-Repository-Services by Sage-Bionetworks.

the class SearchServiceImpl method addReturnDataToHits.

/**
 * Add extra return results to the hit list.
 * @param hits
 * @param includePath
 */
public void addReturnDataToHits(List<Hit> hits, boolean includePath) {
    List<Hit> toRemove = new LinkedList<Hit>();
    if (hits != null) {
        // For each hit we need to add the path
        for (Hit hit : hits) {
            if (includePath) {
                try {
                    EntityPath path = searchDocumentDriver.getEntityPath(hit.getId());
                    hit.setPath(path);
                } catch (NotFoundException e) {
                    // Add a warning and remove it from the hits
                    log.warn("Found a search document that did not exist in the reposiroty: " + hit, e);
                    // We need to remove this from the hits
                    toRemove.add(hit);
                }
            }
        }
    }
    if (!toRemove.isEmpty()) {
        hits.removeAll(toRemove);
    }
}
Also used : Hit(org.sagebionetworks.repo.model.search.Hit) EntityPath(org.sagebionetworks.repo.model.EntityPath) NotFoundException(org.sagebionetworks.repo.web.NotFoundException) LinkedList(java.util.LinkedList)

Example 2 with EntityPath

use of org.sagebionetworks.repo.model.EntityPath in project Synapse-Repository-Services by Sage-Bionetworks.

the class ServletTestHelper method getEntityPath.

/**
 * Get the annotations for an entity
 *
 * @param <T>
 * @param dispatchServlet
 * @param clazz
 * @param id
 * @param userId
 * @return
 * @throws ServletException
 * @throws IOException
 * @throws JSONException
 */
public static <T extends Entity> EntityPath getEntityPath(HttpServlet dispatchServlet, Class<? extends T> clazz, String id, String userId) throws ServletException, IOException, JSONException {
    if (dispatchServlet == null)
        throw new IllegalArgumentException("Servlet cannot be null");
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setMethod("GET");
    request.addHeader("Accept", "application/json");
    request.setRequestURI(UrlHelpers.ENTITY + "/" + id + UrlHelpers.PATH);
    request.setParameter(AuthorizationConstants.USER_ID_PARAM, userId);
    dispatchServlet.service(request, response);
    log.debug("Results: " + response.getContentAsString());
    if (response.getStatus() != HttpStatus.OK.value()) {
        throw new ServletTestHelperException(response);
    }
    return (EntityPath) objectMapper.readValue(response.getContentAsString(), clazz);
}
Also used : EntityPath(org.sagebionetworks.repo.model.EntityPath) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 3 with EntityPath

use of org.sagebionetworks.repo.model.EntityPath in project Synapse-Repository-Services by Sage-Bionetworks.

the class EntityBundleServiceImpl method getEntityBundle.

@Override
public EntityBundle getEntityBundle(String userId, String entityId, Long versionNumber, int mask, HttpServletRequest request) throws NotFoundException, DatastoreException, UnauthorizedException, ACLInheritanceException, ParseException {
    EntityBundle eb = new EntityBundle();
    if ((mask & EntityBundle.ENTITY) > 0) {
        if (versionNumber == null) {
            eb.setEntity(serviceProvider.getEntityService().getEntity(userId, entityId, request));
        } else {
            eb.setEntity(serviceProvider.getEntityService().getEntityForVersion(userId, entityId, versionNumber, request));
        }
    }
    if ((mask & EntityBundle.ANNOTATIONS) > 0) {
        if (versionNumber == null) {
            eb.setAnnotations(serviceProvider.getEntityService().getEntityAnnotations(userId, entityId, request));
        } else {
            eb.setAnnotations(serviceProvider.getEntityService().getEntityAnnotationsForVersion(userId, entityId, versionNumber, request));
        }
    }
    if ((mask & EntityBundle.PERMISSIONS) > 0) {
        eb.setPermissions(serviceProvider.getEntityService().getUserEntityPermissions(userId, entityId));
    }
    if ((mask & EntityBundle.ENTITY_PATH) > 0) {
        List<EntityHeader> path = serviceProvider.getEntityService().getEntityPath(userId, entityId);
        EntityPath ep = new EntityPath();
        ep.setPath(path);
        eb.setPath(ep);
    }
    if ((mask & EntityBundle.ENTITY_REFERENCEDBY) > 0) {
        eb.setReferencedBy(serviceProvider.getEntityService().getEntityReferences(userId, entityId, null, null, null, request));
    }
    if ((mask & EntityBundle.HAS_CHILDREN) > 0) {
        eb.setHasChildren(serviceProvider.getEntityService().doesEntityHaveChildren(userId, entityId, request));
    }
    if ((mask & EntityBundle.ACL) > 0) {
        try {
            eb.setAccessControlList(serviceProvider.getEntityService().getEntityACL(entityId, userId, request));
        } catch (ACLInheritanceException e) {
            // ACL is inherited from benefactor. Set ACL to null.
            eb.setAccessControlList(null);
        }
    }
    if ((mask & EntityBundle.ACCESS_REQUIREMENTS) > 0) {
        eb.setAccessRequirements(serviceProvider.getAccessRequirementService().getAccessRequirements(userId, entityId, request).getResults());
    }
    if ((mask & EntityBundle.UNMET_ACCESS_REQUIREMENTS) > 0) {
        eb.setUnmetAccessRequirements(serviceProvider.getAccessRequirementService().getUnfulfilledAccessRequirements(userId, entityId, request).getResults());
    }
    return eb;
}
Also used : ACLInheritanceException(org.sagebionetworks.repo.model.ACLInheritanceException) EntityPath(org.sagebionetworks.repo.model.EntityPath) EntityHeader(org.sagebionetworks.repo.model.EntityHeader) EntityBundle(org.sagebionetworks.repo.model.EntityBundle)

Example 4 with EntityPath

use of org.sagebionetworks.repo.model.EntityPath in project Synapse-Repository-Services by Sage-Bionetworks.

the class SearchDocumentDriverImplAutowireTest method createFakeEntityPath.

private EntityPath createFakeEntityPath() {
    List<EntityHeader> fakePath = new ArrayList<EntityHeader>();
    EntityHeader eh1 = new EntityHeader();
    eh1.setId("1");
    eh1.setName("One");
    eh1.setType("type");
    fakePath.add(eh1);
    eh1 = new EntityHeader();
    eh1.setId("2");
    eh1.setName("Two");
    eh1.setType("type");
    fakePath.add(eh1);
    EntityPath fakeEntityPath = new EntityPath();
    fakeEntityPath.setPath(fakePath);
    return fakeEntityPath;
}
Also used : EntityPath(org.sagebionetworks.repo.model.EntityPath) EntityHeader(org.sagebionetworks.repo.model.EntityHeader) ArrayList(java.util.ArrayList)

Example 5 with EntityPath

use of org.sagebionetworks.repo.model.EntityPath in project Synapse-Repository-Services by Sage-Bionetworks.

the class SearchDocumentDriverImplAutowireTest method testAnnotations.

/**
 * All non-blob annotations belong in the free text annotations field, some
 * are _also_ facets in the search index
 *
 * @throws Exception
 */
@Test
public void testAnnotations() throws Exception {
    Node node = new Node();
    node.setId("5678");
    node.setParentId("1234");
    node.setETag("0");
    node.setNodeType(EntityType.dataset.name());
    node.setDescription("Test annotations");
    Long nonexistantPrincipalId = 42L;
    node.setCreatedByPrincipalId(nonexistantPrincipalId);
    node.setCreatedOn(new Date());
    node.setModifiedByPrincipalId(nonexistantPrincipalId);
    node.setModifiedOn(new Date());
    node.setVersionLabel("versionLabel");
    NodeRevisionBackup rev = new NodeRevisionBackup();
    NamedAnnotations named = new NamedAnnotations();
    Annotations primaryAnnos = named.getAdditionalAnnotations();
    primaryAnnos.addAnnotation("species", "Dragon");
    primaryAnnos.addAnnotation("numSamples", 999L);
    Annotations additionalAnnos = named.getAdditionalAnnotations();
    additionalAnnos.addAnnotation("Species", "Unicorn");
    additionalAnnos.addAnnotation("stringKey", "a multi-word annotation gets underscores so we can exact-match find it");
    additionalAnnos.addAnnotation("longKey", 10L);
    additionalAnnos.addAnnotation("number_of_samples", "42");
    additionalAnnos.addAnnotation("Tissue_Tumor", "ear lobe");
    additionalAnnos.addAnnotation("platform", "synapse");
    Date dateValue = new Date();
    additionalAnnos.addAnnotation("dateKey", dateValue);
    additionalAnnos.addAnnotation("blobKey", new String("bytes").getBytes());
    rev.setNamedAnnotations(named);
    Set<Reference> references = new HashSet<Reference>();
    Map<String, Set<Reference>> referenceMap = new HashMap<String, Set<Reference>>();
    referenceMap.put("tooMany", references);
    node.setReferences(referenceMap);
    for (int i = 0; i <= SearchDocumentDriverImpl.FIELD_VALUE_SIZE_LIMIT + 10; i++) {
        Reference ref = new Reference();
        ref.setTargetId("123" + i);
        ref.setTargetVersionNumber(1L);
        references.add(ref);
    }
    Set<ACCESS_TYPE> rwAccessType = new HashSet<ACCESS_TYPE>();
    rwAccessType.add(ACCESS_TYPE.READ);
    rwAccessType.add(ACCESS_TYPE.UPDATE);
    ResourceAccess rwResourceAccess = new ResourceAccess();
    // readWriteTest@sagebase.org
    rwResourceAccess.setPrincipalId(123L);
    rwResourceAccess.setAccessType(rwAccessType);
    Set<ACCESS_TYPE> roAccessType = new HashSet<ACCESS_TYPE>();
    roAccessType.add(ACCESS_TYPE.READ);
    ResourceAccess roResourceAccess = new ResourceAccess();
    // readOnlyTest@sagebase.org
    roResourceAccess.setPrincipalId(456L);
    roResourceAccess.setAccessType(roAccessType);
    Set<ResourceAccess> resourceAccesses = new HashSet<ResourceAccess>();
    resourceAccesses.add(rwResourceAccess);
    resourceAccesses.add(roResourceAccess);
    AccessControlList acl = new AccessControlList();
    acl.setResourceAccess(resourceAccesses);
    EntityPath fakeEntityPath = createFakeEntityPath();
    AdapterFactoryImpl adapterFactoryImpl = new AdapterFactoryImpl();
    JSONObjectAdapter adapter = adapterFactoryImpl.createNew();
    fakeEntityPath.writeToJSONObject(adapter);
    String fakeEntityPathJSONString = adapter.toJSONString();
    Document document = searchDocumentDriver.formulateSearchDocument(node, rev, acl, fakeEntityPath);
    assertEquals(DocumentTypeNames.add, document.getType());
    assertEquals("en", document.getLang());
    assertEquals(node.getId(), document.getId());
    assertTrue(0 < document.getVersion());
    DocumentFields fields = document.getFields();
    // Check entity property fields
    assertEquals(node.getId(), fields.getId());
    assertEquals(node.getETag(), fields.getEtag());
    assertEquals(node.getParentId(), fields.getParent_id());
    assertEquals(node.getName(), fields.getName());
    assertEquals("study", fields.getNode_type());
    assertEquals(node.getDescription(), fields.getDescription());
    // since the Principal doesn't exist, the 'created by' display name defaults to the principal ID
    assertEquals("" + nonexistantPrincipalId, fields.getCreated_by());
    assertEquals(new Long(node.getCreatedOn().getTime() / 1000), fields.getCreated_on());
    // since the Principal doesn't exist, the 'modified by' display name defaults to the principal ID
    assertEquals("" + nonexistantPrincipalId, fields.getModified_by());
    assertEquals(new Long(node.getModifiedOn().getTime() / 1000), fields.getModified_on());
    // Check boost field
    assertTrue(fields.getBoost().contains(node.getId()));
    assertTrue(fields.getBoost().contains(node.getName()));
    // Check the faceted fields
    assertEquals(2, fields.getNum_samples().size());
    assertEquals(new Long(42), fields.getNum_samples().get(0));
    assertEquals(new Long(999), fields.getNum_samples().get(1));
    assertEquals(2, fields.getSpecies().size());
    assertEquals("Dragon", fields.getSpecies().get(0));
    assertEquals("Unicorn", fields.getSpecies().get(1));
    assertEquals("ear lobe", fields.getTissue().get(0));
    assertEquals("synapse", fields.getPlatform().get(0));
    // Check ACL fields
    assertEquals(2, fields.getAcl().size());
    assertEquals(1, fields.getUpdate_acl().size());
    // Make sure our references were trimmed
    assertTrue(10 < fields.getReferences().size());
    assertEquals(SearchDocumentDriverImpl.FIELD_VALUE_SIZE_LIMIT, fields.getReferences().size());
}
Also used : AccessControlList(org.sagebionetworks.repo.model.AccessControlList) ResourceAccess(org.sagebionetworks.repo.model.ResourceAccess) DocumentFields(org.sagebionetworks.repo.model.search.DocumentFields) EntityPath(org.sagebionetworks.repo.model.EntityPath) AdapterFactoryImpl(org.sagebionetworks.schema.adapter.org.json.AdapterFactoryImpl) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Reference(org.sagebionetworks.repo.model.Reference) JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) Node(org.sagebionetworks.repo.model.Node) NodeRevisionBackup(org.sagebionetworks.repo.model.NodeRevisionBackup) Document(org.sagebionetworks.repo.model.search.Document) Date(java.util.Date) NamedAnnotations(org.sagebionetworks.repo.model.NamedAnnotations) Annotations(org.sagebionetworks.repo.model.Annotations) ACCESS_TYPE(org.sagebionetworks.repo.model.ACCESS_TYPE) NamedAnnotations(org.sagebionetworks.repo.model.NamedAnnotations) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

EntityPath (org.sagebionetworks.repo.model.EntityPath)21 Test (org.junit.Test)13 EntityHeader (org.sagebionetworks.repo.model.EntityHeader)13 AccessControlList (org.sagebionetworks.repo.model.AccessControlList)6 Annotations (org.sagebionetworks.repo.model.Annotations)6 EntityBundle (org.sagebionetworks.repo.model.EntityBundle)4 Project (org.sagebionetworks.repo.model.Project)4 UserEntityPermissions (org.sagebionetworks.repo.model.auth.UserEntityPermissions)4 JSONObjectAdapter (org.sagebionetworks.schema.adapter.JSONObjectAdapter)4 ArrayList (java.util.ArrayList)3 Node (org.sagebionetworks.repo.model.Node)3 NodeRevisionBackup (org.sagebionetworks.repo.model.NodeRevisionBackup)3 ResourceAccess (org.sagebionetworks.repo.model.ResourceAccess)3 Study (org.sagebionetworks.repo.model.Study)3 Document (org.sagebionetworks.repo.model.search.Document)3 JSONObjectAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)3 Date (java.util.Date)2 HashSet (java.util.HashSet)2 StringEntity (org.apache.http.entity.StringEntity)2 Ignore (org.junit.Ignore)2