cancel
Showing results for 
Search instead for 
Did you mean: 

how to check the file exists or not in the repository

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

I am using alfresco for a quite few days. I have learned many things from forums and Alfresco guide.

But presently I am facing a problem. In the alfresco repository I am able to download, upload, do versioning, search etc etc. but the problem is of duplicate entries. If the file already exists in the repository its obviously throw me an exception saying "DUPLICATE ENTRY".

Here i want to check the existence of file in the repository. For this I tried many things, but could not yield proper results.

Can anybody here help me out in checking the existence of file in the repository. If the file exists then it should throw me user defined error that file already exists, if not simply adding the file into the repository.

I appreciate and thankful to all the users and alfresco engineers who directly or indirectly helped me in learning from the scratch about Alfresco Content Management.

Thanks in advance and waiting for a positive reply

HCL power
4 REPLIES 4

jcustovic
Champ in-the-making
Champ in-the-making
Code below shows how to know if the node with specific name already exists.

try {
   space = new Reference(WORKSPACE_STORE, null, pathToNode); // Example: /app:company_home/cm:folder1/cm:folder11
   Node[] nodeArray = getRepositoryService().get(new Predicate(new Reference[] { space }, WORKSPACE_STORE, null));
   space = nodeArray[0].getReference();
   // If you get to here node already exists
} catch (Exception e) {
   // If Exception is thrown node with that name/path does not exist.
}

hclpower
Champ in-the-making
Champ in-the-making
Thanks a lot jcustovic

I tried the above code, but no results  jcustovic .

Can you please eloborate it clearly

Thankx
HCLPower

jcustovic
Champ in-the-making
Champ in-the-making
For example you want to create a space with name "TestSpace" in company home if it doesn't exist or if it exists just get the reference (you may want to update it).

Reference space = null;
try {
   space = new Reference(WORKSPACE_STORE, null, "/app:company_home/cm:TestSpace");
   Node[] nodeArray = getRepositoryService().get(new Predicate(new Reference[] { space }, WORKSPACE_STORE, null));
   space = nodeArray[0].getReference();
   System.out.println("Space " + space.getPath() + " exists. No need to create it.");
} catch (Exception e) {
   System.out.println("Space doesn't exist… Lets create it.");
   ParentReference companyHome = new ParentReference(WORKSPACE_STORE, null, "/app:company_home", Constants.ASSOC_CONTAINS, null);
   companyHome.setChildName(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, "TestSpace"));

   NamedValue[] properties = new NamedValue[] { Utils.createNamedValue(Constants.PROP_NAME, "TestSpace")) };

   CMLCreate create = new CMLCreate("1", companyHome, null, Constants.ASSOC_CONTAINS, null, spaceTypeQName, properties);
   CML cml = new CML();
   cml.setCreate(new CMLCreate[] { create });

   UpdateResult[] results = getRepositoryService().update(cml);
   System.out.println("Space " + results[0] + " created.");
}
System.out.println("I got reference to " + space.getPath());

hclpower
Champ in-the-making
Champ in-the-making
Thanks Jan Čustović

It really works.
I was wondering can i write code in Exception or not, as i never did in my programming.
Anyways thanks a lott Jan Čustović

Thanks
HCLPower