How to upload same file many times using versioning in java
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-17-2010 12:06 AM
Hi Folks,
Im able to upload and download files normally without any problem using java code from and in Alfresco repository.
But now my requirement is that i want to upload same file with little modifications many times which can be done through versioning in alfresco using java code.
I have done it through System.milliseconds concept, but that is not feasible as its not allowing to download.
Can anybody help me out in writing a java code with version history for uploading and downloading from Alfresco repository.
From forums i came to know that it can be done as the file names will remain same and only the version changes, so that its very easy to download the latest version file.
Thanks in Advance.
Hope i may get a positive reply from alfresco users
HCL POWER
Im able to upload and download files normally without any problem using java code from and in Alfresco repository.
But now my requirement is that i want to upload same file with little modifications many times which can be done through versioning in alfresco using java code.
I have done it through System.milliseconds concept, but that is not feasible as its not allowing to download.
Can anybody help me out in writing a java code with version history for uploading and downloading from Alfresco repository.
From forums i came to know that it can be done as the file names will remain same and only the version changes, so that its very easy to download the latest version file.
Thanks in Advance.
Hope i may get a positive reply from alfresco users

HCL POWER
Labels:
- Labels:
-
Archive
6 REPLIES 6

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-17-2010 01:27 AM
Hi,
Use the below code
String nodeRef = req.getParameter("nodeRef");
ParameterCheck.mandatoryString("nodeRef", nodeRef);
String versionLabel = "1.0";
versionLabel = req.getParameter("versionLabel");
VersionHistory versionHistory = serviceRegistry.getVersionService().getVersionHistory(new NodeRef(nodeRef));
String fileName = (String)this.serviceRegistry.getNodeService().getProperty(new NodeRef(nodeRef), ContentModel.PROP_NAME);
if(null!= versionHistory){
Iterator<Version> versionIterator = versionHistory.getAllVersions().iterator();
Version version;
/*
* Frame the below url to get the contet of specific version
* http://localhost:8080/alfresco/d/d/versionStore/version2Store/551de18e-e94a-4ad1-a867-898d1adb0e22/W...
* the frozenNoderef to be used for the same to get the content of different version
*/
while(versionIterator.hasNext()){
version = versionIterator.next();
if(version.getVersionLabel().equals(versionLabel)){
versionURL = DownloadContentServlet.generateBrowserURL(version.getFrozenStateNodeRef(),fileName);
LOGGER.debug("Frozen nodeRef:::"+version.getFrozenStateNodeRef());
LOGGER.debug("The version URL of the contetn is:::"+versionURL);
}
}
}
Thanks,
Patil
Cignex Technologies
Bangalore
Use the below code
String nodeRef = req.getParameter("nodeRef");
ParameterCheck.mandatoryString("nodeRef", nodeRef);
String versionLabel = "1.0";
versionLabel = req.getParameter("versionLabel");
VersionHistory versionHistory = serviceRegistry.getVersionService().getVersionHistory(new NodeRef(nodeRef));
String fileName = (String)this.serviceRegistry.getNodeService().getProperty(new NodeRef(nodeRef), ContentModel.PROP_NAME);
if(null!= versionHistory){
Iterator<Version> versionIterator = versionHistory.getAllVersions().iterator();
Version version;
/*
* Frame the below url to get the contet of specific version
* http://localhost:8080/alfresco/d/d/versionStore/version2Store/551de18e-e94a-4ad1-a867-898d1adb0e22/W...
* the frozenNoderef to be used for the same to get the content of different version
*/
while(versionIterator.hasNext()){
version = versionIterator.next();
if(version.getVersionLabel().equals(versionLabel)){
versionURL = DownloadContentServlet.generateBrowserURL(version.getFrozenStateNodeRef(),fileName);
LOGGER.debug("Frozen nodeRef:::"+version.getFrozenStateNodeRef());
LOGGER.debug("The version URL of the contetn is:::"+versionURL);
}
}
}
Thanks,
Patil
Cignex Technologies
Bangalore
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-17-2010 04:35 AM
Really thanx Patil for your reply
but i think this code is for downloading right. can you please take some pain in providing upload and the download code with versioning concept.
Thanx in Advance
hcl power
but i think this code is for downloading right. can you please take some pain in providing upload and the download code with versioning concept.
Thanx in Advance

hcl power

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-17-2010 04:42 AM
Just apply versionable aspect to your content and add the content to the node.
Remaining alfresco will take care.
Thanks,
Patil
Cignex Technologies
Bangalore
Remaining alfresco will take care.
Thanks,
Patil
Cignex Technologies
Bangalore

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-17-2010 04:53 AM
FacesContext context = FacesContext.getCurrentInstance();
RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(context);
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>()
{
public Object execute() throws Throwable
{
// add the versionable aspect to the node
getNodeService().addAspect(getDocument().getNodeRef(), ContentModel.ASPECT_VERSIONABLE, null);
return null;
}
};
txnHelper.doInTransaction(callback);
Enjoy
Thanks,
Patil
Cignex Technologies
Bangalore
RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(context);
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>()
{
public Object execute() throws Throwable
{
// add the versionable aspect to the node
getNodeService().addAspect(getDocument().getNodeRef(), ContentModel.ASPECT_VERSIONABLE, null);
return null;
}
};
txnHelper.doInTransaction(callback);
Enjoy
Thanks,
Patil
Cignex Technologies
Bangalore
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-17-2010 06:59 AM
Hi patil,
thanks for your effort buddy.
but let me tell you clearly, im very new to Alfresco. Just gone through some forums and got code in bits and pieces, to which i kept in sequence following Alfresco developer guide.
now the guide is not guiding for versioning. It says to use timeStamp. But you know the results of timeStamp.
i have used the
CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_VERSIONABLE, null, predicate, null);
CML cml = new CML();
cml.setAddAspect(new CMLAddAspect[]{addAspect});
as per ur request. but im still not able to do properly.
if you can provide me the complete steps or the code then it will be very helpful for me.
Thanx in Advance
thanks for your effort buddy.
but let me tell you clearly, im very new to Alfresco. Just gone through some forums and got code in bits and pieces, to which i kept in sequence following Alfresco developer guide.
now the guide is not guiding for versioning. It says to use timeStamp. But you know the results of timeStamp.
i have used the
CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_VERSIONABLE, null, predicate, null);
CML cml = new CML();
cml.setAddAspect(new CMLAddAspect[]{addAspect});
as per ur request. but im still not able to do properly.
if you can provide me the complete steps or the code then it will be very helpful for me.
Thanx in Advance

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-20-2010 04:55 AM
Hi ,
U have to update the cml object created.
Something like this
// Issue CML statement via Repository Web Service and retrieve result
// Note: Batching of multiple statements into a single web call
UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);
Find some code for applying the aspect in WebServices
// Create a reference to the parent where we want to create content
Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
ParentReference companyHomeParent = new ParentReference(storeRef, null, "/app:company_home", Constants.ASSOC_CONTAINS, null);
// Assign name
String name = "Web Services sample (" + System.currentTimeMillis() + ")";
companyHomeParent.setChildName("cm:" + name);
// Construct CML statement to create content node
// Note: Assign "1" as a local id, so we can refer to it in subsequent
// CML statements within the same CML block
NamedValue[] contentProps = new NamedValue[1];
contentProps[0] = Utils.createNamedValue(Constants.PROP_NAME, name);
CMLCreate create = new CMLCreate("1", companyHomeParent, null, null, null, Constants.TYPE_CONTENT, contentProps);
// Construct CML statement to add titled aspect
NamedValue[] titledProps = new NamedValue[2];
titledProps[0] = Utils.createNamedValue(Constants.PROP_TITLE, name);
titledProps[1] = Utils.createNamedValue(Constants.PROP_DESCRIPTION, name);
CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_TITLED, titledProps, null, "1");
// Construct CML Block
CML cml = new CML();
cml.setCreate(new CMLCreate[] {create});
cml.setAddAspect(new CMLAddAspect[] {addAspect});
// Issue CML statement via Repository Web Service and retrieve result
// Note: Batching of multiple statements into a single web call
UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);
Reference content = result[0].getDestination();
//
// Write some content
//
ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
String text = "The quick brown fox jumps over the lazy dog";
ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8");
Content contentRef = contentService.write(content, Constants.PROP_CONTENT, text.getBytes(), contentFormat);
System.out.println("Content Length: " + contentRef.getLength());
WebServiceFactory.getRepositoryService();
Thanks,
Patil
Cignex Technologies
Bangalore
U have to update the cml object created.
Something like this
// Issue CML statement via Repository Web Service and retrieve result
// Note: Batching of multiple statements into a single web call
UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);
Find some code for applying the aspect in WebServices
// Create a reference to the parent where we want to create content
Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
ParentReference companyHomeParent = new ParentReference(storeRef, null, "/app:company_home", Constants.ASSOC_CONTAINS, null);
// Assign name
String name = "Web Services sample (" + System.currentTimeMillis() + ")";
companyHomeParent.setChildName("cm:" + name);
// Construct CML statement to create content node
// Note: Assign "1" as a local id, so we can refer to it in subsequent
// CML statements within the same CML block
NamedValue[] contentProps = new NamedValue[1];
contentProps[0] = Utils.createNamedValue(Constants.PROP_NAME, name);
CMLCreate create = new CMLCreate("1", companyHomeParent, null, null, null, Constants.TYPE_CONTENT, contentProps);
// Construct CML statement to add titled aspect
NamedValue[] titledProps = new NamedValue[2];
titledProps[0] = Utils.createNamedValue(Constants.PROP_TITLE, name);
titledProps[1] = Utils.createNamedValue(Constants.PROP_DESCRIPTION, name);
CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_TITLED, titledProps, null, "1");
// Construct CML Block
CML cml = new CML();
cml.setCreate(new CMLCreate[] {create});
cml.setAddAspect(new CMLAddAspect[] {addAspect});
// Issue CML statement via Repository Web Service and retrieve result
// Note: Batching of multiple statements into a single web call
UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);
Reference content = result[0].getDestination();
//
// Write some content
//
ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
String text = "The quick brown fox jumps over the lazy dog";
ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8");
Content contentRef = contentService.write(content, Constants.PROP_CONTENT, text.getBytes(), contentFormat);
System.out.println("Content Length: " + contentRef.getLength());
WebServiceFactory.getRepositoryService();
Thanks,
Patil
Cignex Technologies
Bangalore
