<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to create directory structure using Java API? in Alfresco Forum</title>
    <link>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103537#M29460</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I'm trying to create a folder structure (something similar to "mkdir -p" command) using Java API, but when I create a folder, I cannot create a subfolder in it, because a parent folder does not exist yet.&lt;/P&gt;&lt;P&gt;How can I create a folder, commit transition, and then create a subfolder?&lt;/P&gt;</description>
    <pubDate>Sun, 08 Nov 2020 14:09:14 GMT</pubDate>
    <dc:creator>upforsin</dc:creator>
    <dc:date>2020-11-08T14:09:14Z</dc:date>
    <item>
      <title>How to create directory structure using Java API?</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103537#M29460</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I'm trying to create a folder structure (something similar to "mkdir -p" command) using Java API, but when I create a folder, I cannot create a subfolder in it, because a parent folder does not exist yet.&lt;/P&gt;&lt;P&gt;How can I create a folder, commit transition, and then create a subfolder?&lt;/P&gt;</description>
      <pubDate>Sun, 08 Nov 2020 14:09:14 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103537#M29460</guid>
      <dc:creator>upforsin</dc:creator>
      <dc:date>2020-11-08T14:09:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to commit a transation using Java API?</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103538#M29461</link>
      <description>&lt;P&gt;Can you provide detail how you are creating?Some&amp;nbsp;code snippet.&lt;/P&gt;&lt;P&gt;If you are using lucene to search created node then it will take some time because indexing will take time for making content searchable.&lt;/P&gt;&lt;P&gt;You can use&amp;nbsp;&lt;A href="https://docs.alfresco.com/5.2/concepts/intrans-metadata.html" target="_self" rel="nofollow noopener noreferrer"&gt;Transactional metadata query&lt;/A&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 03:33:00 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103538#M29461</guid>
      <dc:creator>sp2</dc:creator>
      <dc:date>2020-11-09T03:33:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to commit a transation using Java API?</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103539#M29462</link>
      <description>&lt;P&gt;By java api do you mean the embeded java foundation api or remote java api ?&lt;BR /&gt;If you mean java fondation api ,you shoud inject the capitalized bean name NodeService into your bean,like following&lt;/P&gt;&lt;PRE&gt;&amp;lt;property name="nodeService" ref="NodeService"/&amp;gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;If you mean&amp;nbsp; remote java pai ,you can try createPath method of org.apache.chemistry.opencmis.client.api.Session interface&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 07:02:02 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103539#M29462</guid>
      <dc:creator>kaynezhang</dc:creator>
      <dc:date>2020-11-09T07:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to commit a transation using Java API?</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103540#M29463</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;A href="https://migration33.stage.lithium.com/t5/user/viewprofilepage/user-id/36939"&gt;@sanjaybandhniya&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sure, I do.&amp;nbsp; The method should create a directory structure given a path (similar to mkdir -p command). I used Lucene (is there another way if I don't have noderef, only the human-readable-path?).&lt;/P&gt;&lt;PRE&gt;    private static void createDirectoryStructure(String parentPath, String childName) {
		StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
		ResultSet rs = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE,
				"PATH:\"" + parentPath + "\"");
		if (rs.length() == 0) {
			logger.warn("Didn't find the parent path, trying to create it...");
			createParentPath(parentPath);  // very similar code
			rs = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE,
					"PATH:\"" + parentPath + "\"");
			if(rs.length() == 0) {
				throw new AlfrescoRuntimeException("Parent still does not exist!");
			}
		}
		NodeRef parentRef = rs.getNodeRef(0);&lt;BR /&gt;                ....
    }&lt;/PRE&gt;&lt;P&gt;(I modified the code a bit to make it more readable)&lt;/P&gt;&lt;P&gt;&lt;A href="https://migration33.stage.lithium.com/t5/user/viewprofilepage/user-id/39620"&gt;@kaynezhang&lt;/A&gt;&amp;nbsp;I mean Java (foundation?) API, not remote. Getting nodeService is not a problem at all, I'm just trying to create a node, and then create another node inside the previously created one.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 14:32:28 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103540#M29463</guid>
      <dc:creator>upforsin</dc:creator>
      <dc:date>2020-11-09T14:32:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to commit a transation using Java API?</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103541#M29464</link>
      <description>&lt;P&gt;Considering you have root folder known, i would follow steps as (no external transaction handling needed):&lt;/P&gt;
&lt;P&gt;1- Using the root folder name, get the path of the root folder.&lt;/P&gt;
&lt;P&gt;2- Using the &lt;EM&gt;TMDQ&lt;/EM&gt; as suggested, get the root folder node ref.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3- If subfolder&amp;nbsp; (considering an input&amp;nbsp; are separated by multiple paths (like your mkdir -p case), you can split the path and iterate each subfolder and create subfolder within rootfolder and their sub folders.&lt;/P&gt;
&lt;P&gt;3- If subfolder is not separated by path, you can directly create the subfoler within root folder.&lt;/P&gt;
&lt;P&gt;Here is an example:&lt;/P&gt;
&lt;PRE&gt;String folderPathQuery  = getFolderPathQuery(rootFolderName, siteShortName); //Change the query accordingly if not using sites
List&amp;lt;NodeRef&amp;gt; selectedNodes = searchService.selectNodes(nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE),
		folderPathQuery, null, namespaceService, false);
if (selectedNodes != null &amp;amp;&amp;amp; !selectedNodes.isEmpty()) {
	NodeRef rootFolderRef = selectedNodes.get(0); 
	if(rootFolderRef != null &amp;amp;&amp;amp; fileFolderService.exists(rootFolderRef)) {
		if(subFolderName.contains("/")) {
			String[] pathTokens = subFolderName.split("/");
			NodeRef subRootNodeRef = rootFolderRef;
			for (String subFolder : pathTokens) {
				subRootNodeRef = fileFolderService.create(subRootNodeRef, subFolder, ContentModel.TYPE_FOLDER).getNodeRef();
			}
		} else {
			fileFolderService.create(rootFolderRef, subFolderName, ContentModel.TYPE_FOLDER);
		}
	} else {
		// handle and notify message as needed. ROOT FOLDR NOT FOUND
	}
} else {
	// handle and notify message as needed.
}


private String getFolderPathQuery(String folderPath, String siteShortName) {
	StringBuilder queryBuilder = new StringBuilder("/app:company_home/st:sites/");
	queryBuilder.append("cm:").append(ISO9075.encode(siteShortName))
			.append("/cm:documentLibrary");
	if (null != folderPath &amp;amp;&amp;amp; !folderPath.isEmpty()) {
		String[] pathTokens = folderPath.split("/");
		if (!"Document Library".equals(folderPath)) {
			for (String pathToken : pathTokens) {
				queryBuilder.append("/cm:").append(ISO9075.encode(pathToken));
			}
		}
	}
	return queryBuilder.toString();
}&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Nov 2020 16:24:44 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103541#M29464</guid>
      <dc:creator>abhinavmishra14</dc:creator>
      <dc:date>2020-11-09T16:24:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to commit a transation using Java API?</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103542#M29465</link>
      <description>&lt;P&gt;Well, that is even more than I wanted. Thank you very much.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 17:49:10 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103542#M29465</guid>
      <dc:creator>upforsin</dc:creator>
      <dc:date>2020-11-09T17:49:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to commit a transation using Java API?</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103543#M29466</link>
      <description>&lt;P&gt;&lt;A href="https://migration33.stage.lithium.com/t5/user/viewprofilepage/user-id/8063"&gt;@upforsin&lt;/A&gt;&amp;nbsp;Glad to hear that.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2020 17:55:29 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/how-to-create-directory-structure-using-java-api/m-p/103543#M29466</guid>
      <dc:creator>abhinavmishra14</dc:creator>
      <dc:date>2020-11-09T17:55:29Z</dc:date>
    </item>
  </channel>
</rss>

