Java Code Examples for org.apache.chemistry.opencmis.client.api.Session#query()

The following examples show how to use org.apache.chemistry.opencmis.client.api.Session#query() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CMISDataCreatorTest.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testCreate()
{
    Session session = getSession("admin", "admin");
    
    String folderName = getRootFolderName();
    Folder root = session.getRootFolder();
    
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
    properties.put(PropertyIds.NAME, folderName);

    // create the folder
    Folder newFolder = root.createFolder(properties);
    
    for(int i = 0; i < 50; i++)
    {
        AccessControlPrincipalDataImpl principal = new AccessControlPrincipalDataImpl("user"+i);
        List<String> permissions = new ArrayList<String>(1);
        permissions.add(BasicPermissions.READ);
        List<Ace> addAces = new ArrayList<Ace>(1);
        addAces.add(new AccessControlEntryImpl(principal, permissions));
        newFolder.addAcl(addAces, AclPropagation.PROPAGATE);
        
        Map<String, Object> updateProperties = new HashMap<String, Object>();
        updateProperties.put("cm:title", "Update title "+i);
        newFolder.updateProperties(properties);
        
        if(i % 10 == 0)
        {
            System.out.println("@ "+i);
        }
    }
    ItemIterable<QueryResult> result = session.query("select * from cmis:folder", false);
    assertTrue(result.getTotalNumItems() > 0);
    
    result = session.query("select * from cmis:folder where cmis:name = '"+folderName+"'", false);
    assertTrue(result.getTotalNumItems() > 0);
    
}
 
Example 2
Source File: CMISDataCreatorTest.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testCreateLots() throws Exception
{
    Session session = getSession("admin", "admin");
    
    Folder root = session.getRootFolder();
    String folderNameBase = getRootFolderName();

    
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
    properties.put(PropertyIds.NAME, folderNameBase);
    
    Folder base = root.createFolder(properties);
    for(int i = 0; i < 10; i++)
    {
       AccessControlPrincipalDataImpl principal = new AccessControlPrincipalDataImpl(""+i+i+i);
       List<String> permissions = new ArrayList<String>(1);
       permissions.add(BasicPermissions.ALL);
       List<Ace> addAces = new ArrayList<Ace>(1);
       addAces.add(new AccessControlEntryImpl(principal, permissions));
       base.addAcl(addAces, AclPropagation.PROPAGATE);
    }
    
    
    Thread last = null;
    
    for(int i = 0; i < 10; i++)
    {
        Creator creator = new Creator(base.getPath(), i);
        Thread thread = new Thread(creator);
        thread.start();
        last = thread;
    }
    
    if(last != null)
    {
        last.join();
    }
  
    ItemIterable<QueryResult> result = session.query("select * from cmis:folder", false);
    assertTrue(result.getTotalNumItems() > 0);
    
    //result = session.query("select * from cmis:folder where cmis:name = '"+folderName+"'", false);
    //assertTrue(result.getTotalNumItems() > 0);
    
}
 
Example 3
Source File: CMISDataCreatorTest.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testQueryFolderProperties()
{
    Session session = getSession("admin", "admin");
    
    String folderName = getRootFolderName();
    Folder root = session.getRootFolder();
    
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
    properties.put(PropertyIds.NAME, folderName);

    // create the folder
    Folder newFolder = root.createFolder(properties);
    
    ItemIterable<QueryResult>  result = session.query("select * from cmis:folder where cmis:name = '"+folderName+"'", false);
    assertEquals(1, result.getTotalNumItems());
    
    
    String uniqueName = getUniqueName();
    Map<String, Object> uProperties = new HashMap<String, Object>();
    uProperties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
    uProperties.put(PropertyIds.NAME, uniqueName);
    Folder uniqueFolder = newFolder.createFolder(uProperties);
    
    result = session.query("select * from cmis:folder where cmis:name = '"+uniqueName+"'", false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:folder where cmis:name = '"+uniqueName+"' AND IN_FOLDER('"+ uniqueFolder.getParentId() + "')" , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:folder where IN_FOLDER('"+ uniqueFolder.getParentId() + "')" , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:folder where cmis:name = '"+uniqueName+"' AND cmis:createdBy = '"+ uniqueFolder.getCreatedBy()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:folder where cmis:name = '"+uniqueName+"' AND cmis:objectId = '"+ uniqueFolder.getId()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:folder where cmis:name = '"+uniqueName+"' AND cmis:lastModifiedBy = '"+ uniqueFolder.getLastModifiedBy()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:folder where cmis:name = '"+ uniqueFolder.getName()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    String creationDate = ISO8601DateFormat.format(uniqueFolder.getCreationDate().getTime());
    result = session.query("select * from cmis:folder where cmis:name = '"+uniqueName+"' AND cmis:creationDate = '"+ creationDate +"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    String modificationDate = ISO8601DateFormat.format(uniqueFolder.getLastModificationDate().getTime());
    result = session.query("select * from cmis:folder where cmis:name = '"+uniqueName+"' AND cmis:lastModificationDate = '"+ modificationDate+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:folder where cmis:name = '"+uniqueName+"' AND cmis:objectTypeId = '"+ uniqueFolder.getType().getQueryName()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:folder where cmis:name = '"+uniqueName+"' AND cmis:baseTypeId = '"+ uniqueFolder.getBaseType().getQueryName()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
}
 
Example 4
Source File: CMISDataCreatorTest.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testQueryDocumentProperties() throws Exception
{
    Session session = getSession("admin", "admin");
    
    String folderName = getRootFolderName();
    Folder root = session.getRootFolder();
    
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
    properties.put(PropertyIds.NAME, folderName);

    // create the folder
    Folder newFolder = root.createFolder(properties);
    
    ItemIterable<QueryResult>  result = session.query("select * from cmis:folder where cmis:name = '"+folderName+"'", false);
    assertEquals(1, result.getTotalNumItems());
    
    
    Document uniqueDocument = createUniqueDocument(newFolder);
    
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"'", false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"' AND IN_FOLDER('"+ newFolder.getId() + "')" , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where IN_FOLDER('"+ newFolder.getId() + "')" , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"' AND cmis:createdBy = '"+ uniqueDocument.getCreatedBy()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"' AND cmis:objectId = '"+ uniqueDocument.getId()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"' AND cmis:lastModifiedBy = '"+ uniqueDocument.getLastModifiedBy()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+ uniqueDocument.getName()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    String creationDate = ISO8601DateFormat.format(uniqueDocument.getCreationDate().getTime());
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"' AND cmis:creationDate = '"+ creationDate +"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    String modificationDate = ISO8601DateFormat.format(uniqueDocument.getLastModificationDate().getTime());
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"' AND cmis:lastModificationDate = '"+ modificationDate+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"' AND cmis:objectTypeId = '"+ uniqueDocument.getType().getQueryName()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"' AND cmis:baseTypeId = '"+ uniqueDocument.getBaseType().getQueryName()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"' AND cmis:contentStreamFileName = '"+ uniqueDocument.getContentStreamFileName()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"' AND cmis:contentStreamLength = '"+ uniqueDocument.getContentStreamLength()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+uniqueDocument.getName()+"' AND cmis:contentStreamMimeType = '"+ uniqueDocument.getContentStreamMimeType()+"'"  , false);
    assertEquals(1, result.getTotalNumItems());
}
 
Example 5
Source File: CMISDataCreatorTest.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testQueryForMultipleDocumentProperties() throws Exception
{
    Session session = getSession("admin", "admin");
    
    String folderName = getRootFolderName();
    Folder root = session.getRootFolder();
    
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
    properties.put(PropertyIds.NAME, folderName);

    // create the folder
    Folder newFolder = root.createFolder(properties);
    
    ItemIterable<QueryResult>  result = session.query("select * from cmis:folder where cmis:name = '"+folderName+"'", false);
    assertEquals(1, result.getTotalNumItems());
    
    
    Document document1 = createUniqueDocument(newFolder);
    Document document2 = createUniqueDocument(newFolder);
    
    //result = session.query("SELECT * FROM cmis:folder d join sys:hidden t on  d.cmis:objectId = t.cmis:objectId", false);
    //assertTrue(0 < result.getTotalNumItems());

    //result = session.query("SELECT * FROM cmis:folder d join sys:hidden t on  d.cmis:objectId = t.cmis:objectId where t.sys:cascadeHidden = false", false);
    //assertTrue(0 < result.getTotalNumItems());
     			   
    result = session.query("SELECT * FROM cmis:document d join exif:exif t on  d.cmis:objectId = t.cmis:objectId", false);
    long count = result.getTotalNumItems();
    
    Map<String, Object> props = new HashMap<String, Object>();
    props.put("exif:focalLength", 10.33d);
    ArrayList<String> aspects = new ArrayList<String>();
    aspects.add("P:exif:exif");
    props.put("cmis:secondaryObjectTypeIds", aspects);
    document1.updateProperties(props);

    result = session.query("SELECT * FROM cmis:document d join exif:exif t on  d.cmis:objectId = t.cmis:objectId", false);
    assertEquals(count+1, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+document1.getName()+"' OR  cmis:name = '"+document2.getName()+"'", false);
    assertEquals(2, result.getTotalNumItems());
    
    result = session.query("select * from cmis:document where cmis:name = '"+document1.getName()+"' AND cmis:contentStreamLength = 5 AND cmis:contentStreamMimeType = 'text/plain'", false);
    assertEquals(1, result.getTotalNumItems());
    
    result = session.query("SELECT * FROM cmis:document", false);
    long docCount = result.getTotalNumItems();
    
    result = session.query("SELECT * FROM cmis:document d left outer join exif:exif t on  d.cmis:objectId = t.cmis:objectId", false);
    assertEquals(docCount, result.getTotalNumItems());
}
 
Example 6
Source File: CmisServiceImpl.java    From studio with GNU General Public License v3.0 4 votes vote down vote up
@Override
@HasPermission(type = DefaultPermission.class, action = "search_cmis")
public List<CmisContentItem> search(@ProtectedResourceId(SITE_ID_RESOURCE_ID) String siteId, String cmisRepo,
                                    String searchTerm, String path)
        throws CmisRepositoryNotFoundException, CmisUnavailableException, CmisTimeoutException {
    List<CmisContentItem> toRet = new ArrayList<CmisContentItem>();
    DataSourceRepository repositoryConfig = getConfiguration(siteId, cmisRepo);
    if (repositoryConfig != null) {
        Session session = createCMISSession(repositoryConfig);
        if (session != null) {
            String contentPath = Paths.get(repositoryConfig.getBasePath(), path).toString();
            CmisObject cmisObject = session.getObjectByPath(contentPath);
            if (cmisObject != null) {
                if (CMIS_FOLDER.equals(cmisObject.getBaseTypeId())) {
                    String queryString = CMIS_SEARCH_QUERY.replace(CMIS_SEARCH_QUERY_FOLDER_ID_VARIABLE,
                            cmisObject.getId()).replace(CMIS_SEARCH_QUERY_SEARCH_TERM_VARIABLE, searchTerm);
                    ItemIterable<QueryResult> result = session.query(queryString, false);
                    Iterator<QueryResult> iterator = result.iterator();
                    int count = 0;
                    while (iterator.hasNext()) {
                        CmisContentItem item = new CmisContentItem();
                        QueryResult qr = iterator.next();

                        String contentId = qr.getPropertyById(OBJECT_ID).getFirstValue().toString();
                        StringTokenizer st = new StringTokenizer(contentId, ";");
                        if (st.hasMoreTokens()) {
                            item.setItemId(st.nextToken());
                        }
                        CmisObject qrObject = session.getObject(item.getItemId());
                        org.apache.chemistry.opencmis.client.api.Document cmisDoc =
                                (org.apache.chemistry.opencmis.client.api.Document)qrObject;
                        item.setItemName(cmisDoc.getName());
                        item.setItemPath(cmisDoc.getPaths().get(0));
                        item.setMimeType(cmisDoc.getContentStreamMimeType());
                        item.setSize(cmisDoc.getContentStreamLength());
                        toRet.add(item);
                    }
                }
            }
        }
    }
    return toRet;
}