JavaAPI for assigning content store

cancel
Showing results for 
Search instead for 
Did you mean: 
brijeshnk
Established Member

JavaAPI for assigning content store

Jump to solution

Hi there , 

I was trying to convert  javascript API  code  to  java API  which move files to different content store(' storeB').   We have  storeB defined in  - 'content-store-selector-context.xml'.  We are using Enterrprises version of Alfresco 5.2.  

java script code as follows ,   - Works perfectly fine . its working code. 

 for each (var n in node.children) {
          if (n.isDocument) {
			  
             	       //Apply script for moving files to DMS Store 01   
			n.removeAspect("cm:versionable");
			n.addAspect("cm:storeSelector");
			n.properties['cm:storeName'] = "storeB";	  
			n.save();			  
          }
     }

Java API Code  - But this code is not moving files to 'storeB'.  Is there anything i am missing ? I can see a save method is java script API. Is there any similiar method available in java APIs.  is there anything missing in the code ? 

		List<ChildAssociationRef> children = nodeService.getChildAssocs(dayFolderRef);
		Map<QName, Serializable> aspectsProps = new HashMap<QName, Serializable>(1);
		aspectsProps.put(ContentModel.PROP_STORE_NAME, "storeB");
		LOG.info("Folder::" + dayFolderRef.getId());
		LOG.info("Number of Subfolder to be moved is ::" + children.size());
		for (ChildAssociationRef childAssoc : children) {
			NodeRef childNodeRef = childAssoc.getChildRef();
			if (ContentModel.TYPE_CONTENT.equals(nodeService.getType(childNodeRef))) {
				LOG.info("Moving the file to secondary storae "+childNodeRef.getId());
				nodeService.removeAspect(childNodeRef, ContentModel.ASPECT_VERSIONABLE);
				nodeService.addAspect(childNodeRef, ContentModel.ASPECT_STORE_SELECTOR, aspectsProps);				
				
			}
		}

 

 

Thanks , 

Brijesh 

 

1 Solution

Accepted Solutions
brijeshnk
Established Member

Re: JavaAPI for assigning content store

Jump to solution

the following  trick worked  ,

setting content store name , using setProperty . See code below

nodeService.addAspect(childNodeRef, ContentModel.ASPECT_STORE_SELECTOR , null);
nodeService.setProperty(childNodeRef, ContentModel.PROP_STORE_NAME, "storeB"); 

Still no idea why earlier code failed .

View solution in original post

7 Replies
angelborroy
Alfresco Employee

Re: JavaAPI for assigning content store

Jump to solution

Where are you using that Java code? Inside a behaviour?

There is no need to save in Java API, as it's included on a DB Transaction that will be commited eventually.

Hyland Developer Evangelist
brijeshnk
Established Member

Re: JavaAPI for assigning content store

Jump to solution

@angelborroy  ,

thanks for the reponse. This is an action class triggered by scheduler .   As soon as after executing this i was checking in DB using following query

SELECT * FROM alf_content_url WHERE orphan_time IS NOT NULL; 

Before and after quey result is same.

After execution of above mentioned code, I was exepecting  query  to return higher number  , as number of nodes become orhan.    

brijeshnk
Established Member

Re: JavaAPI for assigning content store

Jump to solution

@angelborroy  ,  you mentioned above 'Transaction that will be commited eventually' . Is there time lag to commit transaction ? is it not immiedately after execting the code or wait for some other process to complete ?  code is executed inside an action method , inititated by scheduler. 

angelborroy
Alfresco Employee

Re: JavaAPI for assigning content store

Jump to solution

The transaction will be commited once the executeJob method is finished.

https://docs.alfresco.com/community/references/dev-extension-points-scheduled-jobs.html

Hyland Developer Evangelist
brijeshnk
Established Member

Re: JavaAPI for assigning content store

Jump to solution

the following  trick worked  ,

setting content store name , using setProperty . See code below

nodeService.addAspect(childNodeRef, ContentModel.ASPECT_STORE_SELECTOR , null);
nodeService.setProperty(childNodeRef, ContentModel.PROP_STORE_NAME, "storeB"); 

Still no idea why earlier code failed .

angelborroy
Alfresco Employee

Re: JavaAPI for assigning content store

Jump to solution

You were adding it also in your JavaScript code, so I guess that it's a required property.

Hyland Developer Evangelist
brijeshnk
Established Member

Re: JavaAPI for assigning content store

Jump to solution

@angelborroy  ,

Agree  its mandatory and  I was adding in java script code , But  I was adding content store property in earlier java code- the one which was not working - please  see below code:

Map<QName, Serializable> aspectsProps = new HashMap<QName, Serializable>(1);
aspectsProps.put(ContentModel.PROP_STORE_NAME, "storeB"); nodeService.removeAspect(childNodeRef, ContentModel.ASPECT_VERSIONABLE); nodeService.addAspect(childNodeRef, ContentModel.ASPECT_STORE_SELECTOR, aspectsProps);

 

But the , following code works . its pretty much same code but the way i set the property is different . I used here setPreperty for setting conent store -


nodeService.removeAspect(childNodeRef, ContentModel.ASPECT_VERSIONABLE);
nodeService.addAspect(childNodeRef, ContentModel.ASPECT_STORE_SELECTOR , null); nodeService.setProperty(childNodeRef, ContentModel.PROP_STORE_NAME, "storeB");