cancel
Showing results for 
Search instead for 
Did you mean: 

Check if a folder exist

chicco0386
Champ on-the-rise
Champ on-the-rise
Hi all,
I've this function that create a folder:
public static Reference createFolder(String folderName) throws RepositoryFault, RemoteException {

        RepositoryServiceSoapBindingStub repoService = WebServiceFactory.getRepositoryService();
        String currentPath = getCompanyHome().getPath();
        String newPath = currentPath.concat("/cm:").concat(folderName);
        Reference folder = getCompanyHome();
        folder.setPath(newPath);
        try {
            Node[] nodeFolder = repoService.get(new Predicate(new Reference[] { folder }, getStore(), null));
            folder = nodeFolder[0].getReference();
            if (log.isDebugEnabled()) {
                log.debug("createFolder, la cartella [" + folderName + "] esiste già");
            }
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("createFolder, la cartella [" + folderName + "] NON esiste, procedo nella sua creazione");
            }
            ParentReference parent = new ParentReference(getStore(), null, ConfigManager.getInstance().getConfigValue("alfresco.percorso.company.home"), Constants.ASSOC_CONTAINS, Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, folderName));
            NamedValue[] properties = new NamedValue[] { Utils.createNamedValue(Constants.PROP_NAME, folderName) };
            CMLCreate cmlCreate = new CMLCreate(folderName, parent, null, null, null, Constants.TYPE_FOLDER, properties);
            CML cml = new CML();
            cml.setCreate(new CMLCreate[] { cmlCreate });
            UpdateResult[] updateResults = repoService.update(cml);
            folder = updateResults[0].getDestination();
        }

        return folder;
    }

It's work fine if the folder doesn't exist or if the name folder to create is without white space.
If the folder name contains white space and the folder already exist, the function say me that the folder doesn't exist and so it goes to create it, but I've the error that the folder is duplicate.

I found that the problem is that the folder name with with space into alfresco is stored with special character.
For example:
if I call the method with this folder name "[Test] Ciao – Pluto" and this folder exist into Alfresco, the function don't find it because the alfresco folder name is store like this "_x005b_Test_x005d__x0020_Ciao_x0020_–_x0020_Pluto".

Now I look if there is a method of Alfresco web service API that convert a string in the alfresco folder name, or if there is another method for look if a folder name (with specials characters) exist into alfresco.

CAN YOU HELP ME?
THANK YOU
6 REPLIES 6

chicco0386
Champ on-the-rise
Champ on-the-rise
My work around for this problem stay in this function, that return the id of the reference if it already exist:

private static String findFolderOrContent(Reference root, String toSearch) {
        String idReference = null;
        RepositoryServiceSoapBindingStub repoService = WebServiceFactory.getRepositoryService();
        try {
            QueryResult results = repoService.queryChildren(root);
            ResultSetRow[] rows = results.getResultSet().getRows();
            ResultSetRow currentRow;
            NamedValue[] colums;
            String currentName;
            String currentValue;
            for (int i = 0; i < rows.length; i++) {
                currentRow = rows[i];
                colums = currentRow.getColumns();
                for (int y = 0; y < colums.length; y++) {
                    currentName = colums[y].getName();
                    if (currentName.equals(Constants.PROP_NAME)) {
                        currentValue = colums[y].getValue();
                        if (currentValue.equals(toSearch)) {
                            log.info("findFolderOrContent, [" + toSearch + "] trovato");
                            log.info("findFolderOrContent, id [" + currentRow.getNode().getId() + "]");
                            idReference = currentRow.getNode().getId();
                        }
                    }
                }
            }
        } catch (RepositoryFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return idReference;
    }

So my old function become:
public static Reference createOrGetId(String name) throws RepositoryFault, RemoteException {
        Reference folder = getCompanyHome();
        try {
            String idFind = findFolderOrContent(folder, name);
            if (idFind != null) {
                if (log.isDebugEnabled()) {
                    log.debug("createOrGetId, la cartella [" + name + "] esiste già");
                }
                folder = new Reference(getStore(), idFind, null);

            }
            else {
                if (log.isDebugEnabled()) {
                    log.debug("createOrGetId, la cartella [" + name + "] NON esiste, procedo nella sua creazione");
                }
                RepositoryServiceSoapBindingStub repoService = WebServiceFactory.getRepositoryService();
                String currentPath = getCompanyHome().getPath();
                String newPath = currentPath.concat("/cm:").concat(name);
                folder.setPath(newPath);
                ParentReference parent = new ParentReference(getStore(), null, ConfigManager.getInstance().getConfigValue("alfresco.percorso.company.home"), Constants.ASSOC_CONTAINS, Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, name));
                NamedValue[] properties = new NamedValue[] { Utils.createNamedValue(Constants.PROP_NAME, name) };
                CMLCreate cmlCreate = new CMLCreate(name, parent, null, null, null, Constants.TYPE_FOLDER, properties);
                CML cml = new CML();
                cml.setCreate(new CMLCreate[] { cmlCreate });
                UpdateResult[] updateResults = repoService.update(cml);
                folder = updateResults[0].getDestination();
            }
        } catch (Exception e) {
            log.error("createOrGetId, Exception ", e);
        }
        log.info("createOrGetId, id [" + folder.getUuid() + "]");
        return folder;
    }

I hope that this can helpfull for others users.

BYE

gyro_gearless
Champ in-the-making
Champ in-the-making
An elaborated guess: try endoding your paths using helper class org.alfresco.webservice.util.ISO9075#encode()

HTH
Gyro

chicco0386
Champ on-the-rise
Champ on-the-rise
An elaborated guess: try endoding your paths using helper class org.alfresco.webservice.util.ISO9075#encode()

HTH
Gyro

WOW, this is exactly I needed.

My solution work, but from the id of the node, I can't get the path of the existing reference, so my first solution with your response works very fine.

Other question, if I want to retrive the reference from id, how I can do?
I try with this code
new Reference(getStore(), id, null);
but I can't retrive the path.

Can you help me please?

Thanks for your response

gyro_gearless
Champ in-the-making
Champ in-the-making
Other question, if I want to retrive the reference from id, how I can do?
I try with this code
new Reference(getStore(), id, null);
but I can't retrive the path.

Hmmm, it should work this way - just found this in a tool i once wrote:


Store  spacesStore = new Store(Constants.WORKSPACE_STORE, "SpacesStore");

Reference companyHome = new Reference(spacesStore, folderId, null);

So what happens in getStore()?

Cheers
GYro

chicco0386
Champ on-the-rise
Champ on-the-rise
getStore() do this command:
Store  spacesStore = new Store(Constants.WORKSPACE_STORE, "SpacesStore");

mrogers
Star Contributor
Star Contributor
Use DotCMIS for access from C#.