cancel
Showing results for 
Search instead for 
Did you mean: 

Retrieve custom aspect using CMIS

narayana_giri
Champ in-the-making
Champ in-the-making
Hi,

Defined aspect per link - http://wiki.alfresco.com/wiki/IngresTutorial_Alfresco_Web_Service_API_for_Java

Attaching the aspect(unique id) for each uploaded document through Java Script as below:
LastDocID = parseInt(space.properties["my:uId"]);

NewDocID = LastDocID + 1;

space.properties["my:uId"] = NewDocID;
space.save();

document.properties["my:uId"] = NewDocID;
document.save();

The above aspect(<aspect name="my:uAspect">  <property name="my:uId">) is defined in the customModel.xml and customModel.xml is included in the custom-model-context.xml.

The above Java Script is added to space as a rule to attach unique id for each upload.

Was able to upload attachment using RepositoryServiceSoapBindingStub and ContentServiceSoapBindingStub and also retrieve the aspect as soon as upload is completed as below:

Store spacesStore = new Store(Constants.WORKSPACE_STORE,"SpacesStore");
         
         String luceneQuery = "PATH:\"/app:company_home/cm:maindir/cm:subdir\" ";
         
         Query query = new Query(Constants.QUERY_LANG_LUCENE, luceneQuery);
         QueryResult queryResult = getRepositoryService().query(spacesStore,   query, false);
         ResultSet resultSet = queryResult.getResultSet();
         ResultSetRow[] results1 = resultSet.getRows();
         
         for (ResultSetRow resultRow : results1) {
            ResultSetRowNode nodeResult = resultRow.getNode();

            String[] aspects = nodeResult.getAspects();
            for(int i=0; i< aspects.length ;i++){
            }
            NamedValue[] aspect_properties1 = resultRow.getColumns();
            for (NamedValue str : aspect_properties1) {
               
               if( str.getName().contains("uId")){
                  aspectId =str.getValue();
                  
               }
            }
      }

Migrated upload code to CMIS(per link -http://forums.alfresco.com/forum/developer-discussions/web-scripts/trouble-creating-folder-web-scrip...).

Can you please suggest the equivalent code(to get the aspect)-QueryResult queryResult = getRepositoryService().query(spacesStore,query, false);

Is it possible to use same Lucene Query without using getRepositoryService as it looks for again authentication/login ticket or is it possible to use CMIS query to get the already defined custom aspect?

Any samples/links will be appreciated.


Thanks.
5 REPLIES 5

kaynezhang
World-Class Innovator
World-Class Innovator
You can use apache chemistry opencmis client and execute cmis query like following,alfresco Aspects are exposed as secondary types

      Session session = getSession(servalUrl, userName, password); 
      ItemIterable<QueryResult> qrs = session.query("SELECT * FROM cmis:document D WHERE CONTAINS(D,'PATH: \"/app:company_home/cm:maindir/cm:subdir//*\"')", false);

      for(QueryResult q : qrs) {
             System.out.println(q.getPropertyValueById(PropertyIds.SECONDARY_OBJECT_TYPE_IDS)); //secondary type is just alfresco aspect
            
            
      }

In order to get the already defined custom aspects,using following code

      List<Tree<ObjectType>> types =session.getTypeDescendants("cmis:secondary", -1, true);
      for(Tree<ObjectType> type:types){
         System.out.println(type.getItem());
      }


What do you mean by " it looks for again authentication/login ticket"?

narayana_giri
Champ in-the-making
Champ in-the-making
H,

Thanks for the reply.

I am getting all the uploaded documents along with aspects in the /app:company_home/cm:maindir/cm:subdir if I try the approach that you suggested. What I want to retrieve is the one newly uploaded document along with aspect.

Her is again how user defined aspects defined:
Defined aspect per link - http://wiki.alfresco.com/wiki/IngresTutorial_Alfresco_Web_Service_API_for_Java

Attaching the aspect(unique id) for each uploaded document through Java Script as below:
LastDocID = parseInt(space.properties["my:uId"]);

NewDocID = LastDocID + 1;

space.properties["my:uId"] = NewDocID;
space.save();

document.properties["my:uId"] = NewDocID;
document.save();

The above aspect( ) is defined in the customModel.xml and customModel.xml is included in the custom-model-context.xml.

The above Java Script is added to space as a rule to attach unique id for each upload.

The upload attachment code that you  suggested in the link -https://forums.alfresco.com/forum/developer-discussions/alfresco-api/cannot-upload-file-larger-4mb-u...:
Map<String, Object> properties = new HashMap<String, Object>();
   
      String serverUrl = "http://localhost:8080/alfresco/service/cmis";
      String userName = "admin";
      String password = "admin";

      SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
      Map<String, String> params = new HashMap<String, String>();
      params.put(SessionParameter.USER, userName);
      params.put(SessionParameter.PASSWORD, password);
      params.put(SessionParameter.ATOMPUB_URL, serverUrl);
      params.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
      params.put(SessionParameter.OBJECT_FACTORY_CLASS,
            "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
      java.util.List<Repository> repos = sessionFactory.getRepositories(params);
      if (repos.isEmpty()) {
         throw new RuntimeException("Server has no repositories!");
      }
      Session session = repos.get(0).createSession();

      properties = new HashMap<String, Object>();
      properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document,P:cm:titled");
      properties.put(PropertyIds.NAME, "test3");
      properties.put(PropertyIds.CREATED_BY, "admin");
      properties.put("cm:title", "Title8");
      properties.put("cm:description", "description8");


         //AlfrescoFolder folder1 = (AlfrescoFolder) session.getRootFolder();
         AlfrescoFolder folder1 = (AlfrescoFolder) session.getObjectByPath("/B2Buy");
         
         //File file = new File("D:/action_guide");
         
         File file = new File("D:\\action_guide.pdf");
         
         InputStream fis = new FileInputStream(file);
         VersioningState vs = VersioningState.MAJOR;
         DataInputStream dis = new DataInputStream(fis);
         byte[] bytes = new byte[(int) file.length()];
         dis.readFully(bytes);
         ContentStream contentStream = new ContentStreamImpl(null, null, "application/pdf",
               new ByteArrayInputStream(bytes));

         AlfrescoDocument newDocument = (AlfrescoDocument) folder1.createDocument(properties, contentStream, vs);
         
         
         String ALFRESCO_EXTENSION_NAMESPACE = "http://www.alfresco.org";
         String SET_ASPECTS = "setAspects";
         String PROPERTIES = "properties";
         String VALUE = "value";
         String PROPERTY_DEFINITION_ID="propertyDefinitionId";
         String PROPERTY_STRING="propertyString";
         
         
         Properties propertiess = new PropertiesImpl();
           List<CmisExtensionElement> extensions = new ArrayList<CmisExtensionElement>();
   
           CmisExtensionElement valueElem = new CmisExtensionElementImpl(ALFRESCO_EXTENSION_NAMESPACE, VALUE, null, "Accounting"); //property value of custom property ra:department is accounting
           List<CmisExtensionElement> valueElems = new ArrayList<CmisExtensionElement>();
           valueElems.add(valueElem);
   
           List<CmisExtensionElement> children = new ArrayList<CmisExtensionElement>();
           Map<String, String> attributes = new HashMap<String, String>();
           attributes.put(PROPERTY_DEFINITION_ID, "my:uniqueIdd");//property name is ra:department
           children.add(new CmisExtensionElementImpl(ALFRESCO_EXTENSION_NAMESPACE, PROPERTY_STRING, attributes, valueElems));
   
   
           List<CmisExtensionElement> propertyValuesExtension = new ArrayList<CmisExtensionElement>();
           propertyValuesExtension.add(new CmisExtensionElementImpl(ALFRESCO_EXTENSION_NAMESPACE, PROPERTIES, null, children));
   
   
           CmisExtensionElement setAspectsExtension = new CmisExtensionElementImpl(ALFRESCO_EXTENSION_NAMESPACE, SET_ASPECTS, null, propertyValuesExtension);
           extensions.add(setAspectsExtension);
           System.out.println(extensions);
           propertiess.setExtensions(extensions);
           String repId = session.getRepositoryInfo().getId();
           Holder<String> objectIdHolder = new Holder<String>(newDocument.getId());
           session.getBinding().getObjectService().updateProperties(repId, objectIdHolder, null, propertiess, null);
          
           Collection<ObjectType> list =  newDocument.getAspects();
         //Session session = getSession(servalUrl, userName, password); 
         ItemIterable<QueryResult> qrs = session.query("SELECT * FROM cmis:document D WHERE CONTAINS(D,'PATH: \"/app:company_home/cm:maindir//*\"')", false);
         List listt =null;
         System.out.println("cnt isssssss "+qrs.getTotalNumItems());
         for(QueryResult q : qrs) {

         listt =  q.getProperties();
         

         }
         
         

Please let me know how to retrieve the newly uploaded attachment along with the user defined aspect.


Appreciate your help!.


kaynezhang
World-Class Innovator
World-Class Innovator
If you are using cmis 1.1,you don't need to use cmis extension(CmisExtensionElement ).
After you upload a document,please refresh it and you'll get all second types(aspects).
like following

AlfrescoDocument newDocument = (AlfrescoDocument) folder1.createDocument(properties, contentStream, vs);
newDocument.refresh();
newDocument.getPropertyValue(PropertyIds.SECONDARY_OBJECT_TYPE_IDS);

Hi ,

Again thanks for your help.

Able to retrieve my own custom aspects as below:

List<Property<?>> proList = newDocument.getProperties();
         for(int i=0;i<proList.size();i++){
            Property pro = proList.get(i);
            if(pro.getQueryName().equalsIgnoreCase("my:uId")){
            String aspectId  =pro.getValueAsString();    
            
            }
            
         }   

Question:
AlfrescoFolder folder1 = (AlfrescoFolder) session.getObjectByPath("/dir1/dir2");

Getting the exception:
Exception in thread "main" org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException: Not Found
   at org.apache.chemistry.opencmis.client.bindings.spi.atompub.AbstractAtomPubService.convertStatusCode(AbstractAtomPubService.java:430)
   at org.apache.chemistry.opencmis.client.bindings.spi.atompub.AbstractAtomPubService.read(AbstractAtomPubService.java:552)
   at org.apache.chemistry.opencmis.client.bindings.spi.atompub.AbstractAtomPubService.getObjectInternal(AbstractAtomPubService.java:776)
   at org.apache.chemistry.opencmis.client.bindings.spi.atompub.ObjectServiceImpl.getObjectByPath(ObjectServiceImpl.java:479)
   at org.apache.chemistry.opencmis.client.runtime.SessionImpl.getObjectByPath(SessionImpl.java:430)
   at org.apache.chemistry.opencmis.client.runtime.SessionImpl.getObjectByPath(SessionImpl.java:408)
   at com.fedbid.attach.services.CreateSession.main(CreateSession.java:60)


Tried other options as below:
AlfrescoFolder folder1 = (AlfrescoFolder) session.getObjectByPath("/B2Buy/Test/");
AlfrescoFolder folder1 = (AlfrescoFolder) session.getObjectByPath("/cm:B2Buy/cm:Test");
AlfrescoFolder folder1 = (AlfrescoFolder) session.getObjectByPath("/cmis:B2Buy/cmis:Test");
Folder folder1 = (Folder) session.getObjectByPath("/B2Buy/Test");
Folder folder1 = (Folder) session.getObjectByPath("/B2Buy/Test/");

Folder folder1 = (Folder) session.getObjectByPath("/cm:B2Buy/cm:Test");
Folder folder1 = (Folder) session.getObjectByPath("/cm:B2Buy/cm:Test/");

My requirement is to upload the attachment in the sub folder. Please let me know how to get the session.getObjectByPath()for sub dir.


kaynezhang
World-Class Innovator
World-Class Innovator
Ok,good luck