cancel
Showing results for 
Search instead for 
Did you mean: 

java code to upload content with version aspects

hclpower
Champ in-the-making
Champ in-the-making
Hi Everyone,

Im new to Alfresco and I have been working on Alfresco Repository using java code(servlets) from few days.
I guess this is the best free service provided by Alfresco to upload and download files or contents from and into the Repository.
I need code based on java(servlets) to upload files with versioning concept.

here is the case
I am uploading documents into the repository. But when i make some changes to it and want to re-upload, it throws me an error, saying Duplicate Entry.
Can anyone help me out in explaining me the version concept wherein i can maintain different versions whenever changes are made.Please can anyone provide me the code to solve my problem. I tried my ways to make it versionable but all in vain.

hoping for a positive reply from community members and Alfresco Engineers.

Thanks in Advance
HCLPOWER
Fresher
6 REPLIES 6

jcustovic
Champ in-the-making
Champ in-the-making
If you want to create a new document version here is the code you could use:
Predicate itemsToCheckOut = new Predicate(new Reference[] { node.getReference() }, null, null);
CheckoutResult checkOutResult = getAuthoringService().checkout(itemsToCheckOut, null);
       
Reference workingCopyReference = checkOutResult.getWorkingCopies()[0];
ContentFormat format = new ContentFormat(mimeType, "UTF-8");
getContentService().write(workingCopyReference, Constants.PROP_CONTENT, contentInBytes, format);
       
Predicate predicate = new Predicate(new Reference[] { workingCopyReference }, null, null);
CheckinResult result = getAuthoringService().checkin(predicate, properties, false);

And if you want to create a new versionable document you must apply versionable aspect. Example:
CMLCreate create = new CMLCreate("1", parent, null, Constants.ASSOC_CONTAINS, null, documentTypeQName, properties);
CML cml = new CML();
CMLAddAspect[] aspectArray = new CMLAddAspect[] {new CMLAddAspect(Constants.ASPECT_VERSIONABLE, new NamedValue[] { new NamedValue(PropertyNames.PROP_AUTO_VERSION_ON_UPDATE_PROPS, false, "true", null) }, null, "1");};
cml.setAddAspect(aspectArray);
cml.setCreate(new CMLCreate[] { create });
UpdateResult[] results = getRepositoryService().update(cml);

hclpower
Champ in-the-making
Champ in-the-making
Thanks Jan Čustović.
Here can you clarify what actually is PropertyNames in the below line
CMLAddAspect[] aspectArray = new CMLAddAspect[] {new CMLAddAspect(Constants.ASPECT_VERSIONABLE, new NamedValue[] { new NamedValue(PropertyNames.PROP_AUTO_VERSION_ON_UPDATE_PROPS, false, "true", null) }, null, "1")};

I am not able to get this. Please can you clarify.

Regards,
HCLPower

jcustovic
Champ in-the-making
Champ in-the-making
That is my static class that holds some usual QNames. You can find this property ContentModel.PROP_AUTO_VERSION_PROPS or you can just put String representation "{http://www.alfresco.org/model/content/1.0}autoVersionOnUpdateProps".

hclpower
Champ in-the-making
Champ in-the-making
Really Thanx Jan Čustović.
But still im not successful Smiley Sad 
Donno which is the correct format or sequence. i am facing still problems(errors).

can you please forward me the whole code if its available with you.

Thanks in advance to Mr.Jan Čustović

Regards,
Hcl Power

hclpower
Champ in-the-making
Champ in-the-making
Hmmm i tried this code, it needs 2 more classes
SampleBase.java
ContentReadAndWrite.java

After this, the files are getting stored into the respository with time in MilliSeconds.
This process is creating chaos that which is the latest file to be downloaded. So download problem arising.

I am looking for a code which stores the file as for example say : example.txt
then it should be stored into repository when i again upload with modifications as :example1.0.txt and so on…(same file name but with version added to it)

or atleast overwrite the file with necessary changes made into the repository, so that it can be downloaded easily.

can you please help me out in this issueSmiley Happy

Thanks and Regards,
HCL Power