You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by jl...@apache.org on 2017/12/10 20:10:11 UTC

svn commit: r1817726 - /ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/ConvertTree.java

Author: jleroux
Date: Sun Dec 10 20:10:11 2017
New Revision: 1817726

URL: http://svn.apache.org/viewvc?rev=1817726&view=rev
Log:
No functional changes. 

Refactors (several things), formats and fixes (HOME_DUCUMENT seemed not good) 
the convertTree() method.

This is not used OOTB, so not quite sure how to test it, not documented enough.
I only found that in the initial hansJs commit: "Add a content navigation tree, 
demo data and including a DOS tree conversion program to be able to mimic an 
office file server"

 
    

Modified:
    ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/ConvertTree.java

Modified: ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/ConvertTree.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/ConvertTree.java?rev=1817726&r1=1817725&r2=1817726&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/ConvertTree.java (original)
+++ ofbiz/ofbiz-framework/trunk/applications/content/src/main/java/org/apache/ofbiz/content/ConvertTree.java Sun Dec 10 20:10:11 2017
@@ -19,10 +19,8 @@
 package org.apache.ofbiz.content;
 
 import java.io.BufferedReader;
-import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.IOException;
-import java.io.InputStreamReader;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -31,7 +29,6 @@ import java.util.Map;
 
 import org.apache.ofbiz.base.util.Debug;
 import org.apache.ofbiz.base.util.UtilDateTime;
-import org.apache.ofbiz.base.util.UtilIO;
 import org.apache.ofbiz.base.util.UtilMisc;
 import org.apache.ofbiz.base.util.UtilProperties;
 import org.apache.ofbiz.base.util.UtilValidate;
@@ -56,7 +53,7 @@ With this program and the content naviga
 document tree in OFBiz. From that point on the documents can be connected to the cutomers,
 orders, invoices etc..
 
-In order ta make this service active add the following to the service definition file:
+In order to make this service active add the following to the service definition file:
 
 <service name="convertTree"  auth="true" engine="java" invoke="convertTree" transaction-timeout="3600"
                  location="org.apache.ofbiz.content.tree.ConvertTree">
@@ -67,7 +64,6 @@ In order ta make this service active add
 
 */
 
-
     public static  Map<String, Object> convertTree(DispatchContext dctx, Map<String, ? extends Object> context) {
         Delegator delegator = dctx.getDelegator();
         LocalDispatcher dispatcher = dctx.getDispatcher();
@@ -76,176 +72,158 @@ In order ta make this service active add
         String file = (String) context.get("file");
         String errMsg = "", sucMsg= "";
         GenericValue Entity = null;
-        try {
-            BufferedReader input = null;
-            try {
-                if (UtilValidate.isNotEmpty(file)) {
-                    input = new BufferedReader(new InputStreamReader(new FileInputStream(file), UtilIO.getUtf8()));
-                    String line = null;
-                    int size = 0;
-                    if (file != null) {
-                        int counterLine = 0;
-                        //Home Document
-                        Entity = null;
-                        Entity = delegator.makeValue("Content");
-                        Entity.set("contentId", "ROOT");
-                        Entity.set("contentName", "ROOT");
-                        Entity.set("contentTypeId", "DOCUMENT");
-                        Entity.set("createdByUserLogin", userLogin.get("userLoginId"));
-                        Entity.set("lastModifiedByUserLogin", userLogin.get("userLoginId"));
-                        Entity.set("createdDate", UtilDateTime.nowTimestamp());
-                        Entity.set("lastUpdatedStamp", UtilDateTime.nowTimestamp());
-                        Entity.set("lastUpdatedTxStamp", UtilDateTime.nowTimestamp());
-                        Entity.set("createdStamp", UtilDateTime.nowTimestamp());
-                        Entity.set("createdTxStamp", UtilDateTime.nowTimestamp());
-                        delegator.create(Entity);
-
-                        Entity = null;
-                        Entity = delegator.makeValue("Content");
-                        Entity.set("contentId", "HOME_DUCUMENT");
-                        Entity.set("contentName", "Home");
-                        Entity.set("contentTypeId", "DOCUMENT");
-                        Entity.set("createdByUserLogin", userLogin.get("userLoginId"));
-                        Entity.set("lastModifiedByUserLogin", userLogin.get("userLoginId"));
-                        Entity.set("createdDate", UtilDateTime.nowTimestamp());
-                        Entity.set("lastUpdatedStamp", UtilDateTime.nowTimestamp());
-                        Entity.set("lastUpdatedTxStamp", UtilDateTime.nowTimestamp());
-                        Entity.set("createdStamp", UtilDateTime.nowTimestamp());
-                        Entity.set("createdTxStamp", UtilDateTime.nowTimestamp());
-                        delegator.create(Entity);
-
-                        Map<String, Object> contentAssoc = new HashMap<String, Object>();
-                        contentAssoc.put("contentId", "HOME_DUCUMENT");
-                        contentAssoc.put("contentAssocTypeId", "TREE_CHILD");
-                        contentAssoc.put("contentIdTo", "ROOT");
-                        contentAssoc.put("userLogin", userLogin);
-                        dispatcher.runSync("createContentAssoc", contentAssoc);
-                        while ((line = input.readLine()) != null) {//start line
-                             boolean hasFolder = true;
-                             String rootContent = null, contentId = null; counterLine++;
-                             if (counterLine > 1) {
-                                size = line.length();
-                                String check = "\\", checkSubContent = ",", contentName = "", contentNameInprogress = "", data = line.substring(3, size);
-                                size = data.length();
-
-                                for (int index = 0; index< size; index++) {//start character in line
-                                    boolean contentNameMatch = false;
-                                    int contentAssocSize = 0;
-                                    List<GenericValue> contentAssocs = null;
-                                    if (data.charAt(index) == check.charAt(0) || data.charAt(index) == checkSubContent.charAt(0)) {//store data
-                                        contentName = contentName + contentNameInprogress;
-                                        if (contentName.length() > 100) {
-                                            contentName = contentName.substring(0, 100);
-                                        }
-                                        //check duplicate folder
-                                        GenericValue content = EntityQuery.use(delegator).from("Content").where("contentName", contentName).queryFirst();
-                                        if (content != null) {
-                                            contentId = content.getString("contentId");
-                                        }
-                                        if (content != null && hasFolder==true) {
-                                            if (rootContent != null) {
-                                                contentAssocs = EntityQuery.use(delegator).from("ContentAssoc")
-                                                        .where("contentId", contentId, "contentIdTo", rootContent)
-                                                        .queryList();
-                                                List<GenericValue> contentAssocCheck = EntityQuery.use(delegator).from("ContentAssoc").where("contentIdTo", rootContent).queryList();
-
-                                                Iterator<GenericValue> contentAssChecks = contentAssocCheck.iterator();
-                                                while (contentAssChecks.hasNext() && contentNameMatch == false) {
-                                                    GenericValue contentAss = contentAssChecks.next();
-                                                    GenericValue contentcheck = EntityQuery.use(delegator).from("Content").where("contentId", contentAss.get("contentId")).queryOne();
-                                                    if (contentcheck!=null) {
-                                                        if (contentcheck.get("contentName").equals(contentName)) {
-                                                            contentNameMatch = true;
-                                                            contentId = contentcheck.get("contentId").toString();
-                                                        }
-                                                    }
+        if (UtilValidate.isNotEmpty(file)) {
+            try (BufferedReader input = new BufferedReader(new FileReader(file))) {
+                String line = null;
+                int size = 0;
+                int counterLine = 0;
+                //Home Document
+                Entity = null;
+                Entity = delegator.makeValue("Content");
+                Entity.set("contentId", "ROOT");
+                Entity.set("contentName", "ROOT");
+                Entity.set("contentTypeId", "DOCUMENT");
+                Entity.set("createdByUserLogin", userLogin.get("userLoginId"));
+                Entity.set("lastModifiedByUserLogin", userLogin.get("userLoginId"));
+                Entity.set("createdDate", UtilDateTime.nowTimestamp());
+                Entity.set("lastUpdatedStamp", UtilDateTime.nowTimestamp());
+                Entity.set("lastUpdatedTxStamp", UtilDateTime.nowTimestamp());
+                Entity.set("createdStamp", UtilDateTime.nowTimestamp());
+                Entity.set("createdTxStamp", UtilDateTime.nowTimestamp());
+                delegator.create(Entity);
+
+                Entity = null;
+                Entity = delegator.makeValue("Content");
+                Entity.set("contentId", "HOME_DOCUMENT");
+                Entity.set("contentName", "Home");
+                Entity.set("contentTypeId", "DOCUMENT");
+                Entity.set("createdByUserLogin", userLogin.get("userLoginId"));
+                Entity.set("lastModifiedByUserLogin", userLogin.get("userLoginId"));
+                Entity.set("createdDate", UtilDateTime.nowTimestamp());
+                Entity.set("lastUpdatedStamp", UtilDateTime.nowTimestamp());
+                Entity.set("lastUpdatedTxStamp", UtilDateTime.nowTimestamp());
+                Entity.set("createdStamp", UtilDateTime.nowTimestamp());
+                Entity.set("createdTxStamp", UtilDateTime.nowTimestamp());
+                delegator.create(Entity);
+
+                Map<String, Object> contentAssoc = new HashMap<String, Object>();
+                contentAssoc.put("contentId", "HOME_DOCUMENT");
+                contentAssoc.put("contentAssocTypeId", "TREE_CHILD");
+                contentAssoc.put("contentIdTo", "ROOT");
+                contentAssoc.put("userLogin", userLogin);
+                dispatcher.runSync("createContentAssoc", contentAssoc);
+                while ((line = input.readLine()) != null) {//start line
+                    boolean hasFolder = true;
+                    String rootContent = null, contentId = null; counterLine++;
+                    if (counterLine > 1) {
+                        size = line.length();
+                        String check = "\\", checkSubContent = ",", contentName = "", contentNameInprogress = "", data = line.substring(3, size);
+                        size = data.length();
+
+                        for (int index = 0; index< size; index++) {//start character in line
+                            boolean contentNameMatch = false;
+                            int contentAssocSize = 0;
+                            List<GenericValue> contentAssocs = null;
+                            if (data.charAt(index) == check.charAt(0) || data.charAt(index) == checkSubContent.charAt(0)) {//store data
+                                contentName = contentName + contentNameInprogress;
+                                if (contentName.length() > 100) {
+                                    contentName = contentName.substring(0, 100);
+                                }
+                                //check duplicate folder
+                                GenericValue content = EntityQuery.use(delegator).from("Content").where("contentName", contentName).queryFirst();
+                                if (content != null) {
+                                    contentId = content.getString("contentId");
+                                }
+                                if (content != null && hasFolder==true) {
+                                    if (rootContent != null) {
+                                        contentAssocs = EntityQuery.use(delegator).from("ContentAssoc")
+                                                .where("contentId", contentId, "contentIdTo", rootContent)
+                                                .queryList();
+                                        List<GenericValue> contentAssocCheck = EntityQuery.use(delegator).from("ContentAssoc").where("contentIdTo", rootContent).queryList();
+
+                                        Iterator<GenericValue> contentAssChecks = contentAssocCheck.iterator();
+                                        while (contentAssChecks.hasNext() && contentNameMatch == false) {
+                                            GenericValue contentAss = contentAssChecks.next();
+                                            GenericValue contentcheck = EntityQuery.use(delegator).from("Content").where("contentId", contentAss.get("contentId")).queryOne();
+                                            if (contentcheck!=null) {
+                                                if (contentcheck.get("contentName").equals(contentName) && contentNameMatch == false) {
+                                                    contentNameMatch = true;
+                                                    contentId = contentcheck.get("contentId").toString();
                                                 }
-                                            } else {
-                                                rootContent = "HOME_DUCUMENT";
-                                                contentAssocs = EntityQuery.use(delegator).from("ContentAssoc")
-                                                        .where("contentId", contentId, "contentIdTo", rootContent)
-                                                        .queryList();
                                             }
-                                            contentAssocSize = contentAssocs.size();
-                                        }
-
-                                        if (contentAssocSize == 0 && contentNameMatch == false) {//New Root Content
-                                            Entity = null;
-                                            contentId = delegator.getNextSeqId("Content");
-                                            Entity = delegator.makeValue("Content");
-                                            Entity.set("contentId", contentId);
-                                            Entity.set("contentName", contentName);
-                                            Entity.set("contentTypeId", "DOCUMENT");
-                                            Entity.set("createdByUserLogin", userLogin.get("userLoginId"));
-                                            Entity.set("lastModifiedByUserLogin", userLogin.get("userLoginId"));
-                                            Entity.set("createdDate", UtilDateTime.nowTimestamp());
-                                            delegator.create(Entity);
-                                            hasFolder = false;
-                                        } else {
-                                            hasFolder = true;
-                                        }
-                                        //Relation Content
-                                        if (rootContent == null) {
-                                            rootContent = "HOME_DUCUMENT";
                                         }
+                                    } else {
+                                        rootContent = "HOME_DOCUMENT";
                                         contentAssocs = EntityQuery.use(delegator).from("ContentAssoc")
-                                                .where("contentId", contentId, 
-                                                        "contentIdTo", rootContent,
-                                                        "contentAssocTypeId", "TREE_CHILD")
+                                                .where("contentId", contentId, "contentIdTo", rootContent)
                                                 .queryList();
-
-                                        if (contentAssocs.size() == 0) {
-                                            contentAssoc = new HashMap<String, Object>();
-                                            contentAssoc.put("contentId", contentId);
-                                            contentAssoc.put("contentAssocTypeId", "TREE_CHILD");
-                                            contentAssoc.put("contentIdTo", rootContent);
-                                            contentAssoc.put("userLogin", userLogin);
-                                            dispatcher.runSync("createContentAssoc", contentAssoc);
-                                            rootContent = contentId;
-                                        } else {
-                                            //Debug.logInfo("ContentAssoc [contentId= " + contentId + ", contentIdTo=" + rootContent + "] already exist.");//ShoW log file
-                                            rootContent=contentId;
-                                        }
-                                        contentName = "";
-                                        contentNameInprogress ="";
-                                    }
-                                    if (data.charAt(index)== checkSubContent.charAt(0)) {//Have sub content
-                                        createSubContent(index, data, rootContent, context, dctx);
-                                        index = size;
-                                        continue;
-                                    }
-                                    if ((data.charAt(index)) != check.charAt(0)) {
-                                        contentNameInprogress = contentNameInprogress.concat(Character.toString(data.charAt(index)));
-                                        if (contentNameInprogress.length() > 99) {
-                                            contentName = contentName + contentNameInprogress;
-                                            contentNameInprogress ="";
-                                        }
                                     }
-                                }//end character in line
+                                    contentAssocSize = contentAssocs.size();
+                                }
+
+                                if (contentAssocSize == 0 && contentNameMatch == false) {//New Root Content
+                                    Entity = null;
+                                    contentId = delegator.getNextSeqId("Content");
+                                    Entity = delegator.makeValue("Content");
+                                    Entity.set("contentId", contentId);
+                                    Entity.set("contentName", contentName);
+                                    Entity.set("contentTypeId", "DOCUMENT");
+                                    Entity.set("createdByUserLogin", userLogin.get("userLoginId"));
+                                    Entity.set("lastModifiedByUserLogin", userLogin.get("userLoginId"));
+                                    Entity.set("createdDate", UtilDateTime.nowTimestamp());
+                                    delegator.create(Entity);
+                                    hasFolder = false;
+                                } else {
+                                    hasFolder = true;
+                                }
+                                //Relation Content
+                                if (rootContent == null) {
+                                    rootContent = "HOME_DOCUMENT";
+                                }
+                                contentAssocs = EntityQuery.use(delegator).from("ContentAssoc")
+                                        .where("contentId", contentId, 
+                                                "contentIdTo", rootContent,
+                                                "contentAssocTypeId", "TREE_CHILD")
+                                        .queryList();
+
+                                if (contentAssocs.size() == 0) {
+                                    contentAssoc = new HashMap<String, Object>();
+                                    contentAssoc.put("contentId", contentId);
+                                    contentAssoc.put("contentAssocTypeId", "TREE_CHILD");
+                                    contentAssoc.put("contentIdTo", rootContent);
+                                    contentAssoc.put("userLogin", userLogin);
+                                    dispatcher.runSync("createContentAssoc", contentAssoc);
+                                    rootContent = contentId;
+                                } else {
+                                    //Debug.logInfo("ContentAssoc [contentId= " + contentId + ", contentIdTo=" + rootContent + "] already exist.");//ShoW log file
+                                    rootContent=contentId;
+                                }
+                                contentName = "";
+                                contentNameInprogress ="";
+                            }
+                            if (data.charAt(index)== checkSubContent.charAt(0)) {//Have sub content
+                                createSubContent(index, data, rootContent, context, dctx);
+                                index = size;
+                                continue;
+                            }
+                            if ((data.charAt(index)) != check.charAt(0)) {
+                                contentNameInprogress = contentNameInprogress.concat(Character.toString(data.charAt(index)));
+                                if (contentNameInprogress.length() > 99) {
+                                    contentName = contentName + contentNameInprogress;
+                                    contentNameInprogress ="";
+                                }
                             }
-                        }//end line
-                        sucMsg = UtilProperties.getMessage("ContentUiLabels", "ContentConvertDocumentsTreeSuccessful", UtilMisc.toMap("counterLine", counterLine), locale);
+                        }//end character in line
                     }
-                }
-             }
-             finally {
-                 if (input != null) input.close();
-             }
-             return ServiceUtil.returnSuccess(sucMsg);
-        } catch (IOException e) {
-            errMsg = "IOException "+ UtilMisc.toMap("errMessage", e.toString());
-            Debug.logError(e, errMsg, module);
-            return ServiceUtil.returnError(errMsg);
-        } catch (GenericServiceException e) {
-            errMsg = "GenericServiceException "+ UtilMisc.toMap("errMessage", e.toString());
-            Debug.logError(e, errMsg, module);
-            return ServiceUtil.returnError(errMsg);
-        } catch (GenericEntityException e) {
-            errMsg = "GenericEntityException "+ UtilMisc.toMap("errMessage", e.toString());
-            Debug.logError(e, errMsg, module);
-            e.printStackTrace();
-            return ServiceUtil.returnError(errMsg);
+                }//end line
+                sucMsg = UtilProperties.getMessage("ContentUiLabels", "ContentConvertDocumentsTreeSuccessful", UtilMisc.toMap("counterLine", counterLine), locale);
+            } catch (IOException | GenericServiceException | GenericEntityException e) {
+                errMsg = "Exception " + UtilMisc.toMap("errMessage", e.toString());
+                Debug.logError(e, errMsg, module);
+                return ServiceUtil.returnError(errMsg);
+            }
         }
+        return ServiceUtil.returnSuccess(sucMsg);
     }
 
     public static Map<String,Object> createSubContent(int index, String line, String rootContent, Map<String, ? extends Object> context, DispatchContext dctx) {