-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathconvertree.java
More file actions
367 lines (349 loc) · 22.3 KB
/
convertree.java
File metadata and controls
367 lines (349 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package org.apache.ofbiz.content;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.List;
import java.util.Map;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.UtilDateTime;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.base.util.UtilProperties;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericEntityException;
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.entity.util.EntityQuery;
import org.apache.ofbiz.service.DispatchContext;
import org.apache.ofbiz.service.GenericServiceException;
import org.apache.ofbiz.service.LocalDispatcher;
import org.apache.ofbiz.service.ServiceUtil;
public class ConvertTree{
public static final String module = ConvertTree.class.getName();
/*
This program will convert the output of the DOS 'tree' command into a contantAssoc tree.
the leaves in the tree will point to filenames on the local disk.
With this program and the content navigation a office file server can be replaced with a
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:
<service name="convertTree" auth="true" engine="java" invoke="convertTree" transaction-timeout="3600"
location="org.apache.ofbiz.content.tree.ConvertTree">
<description>Convert DOS tree output to ContentAssoc tree.</description>
<attribute name="file" type="String" mode="IN" optional="false"/>
</service>
*/
public static Map<String, Object> convertTree(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
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 FileReader(file));
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);
int recordCount = 0;
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";
}
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 character in line
recordCount++;
}
}//end line
sucMsg = UtilProperties.getMessage("ContentUiLabels", "ContentConvertDocumentsTreeSuccessful", UtilMisc.toMap("counterLine", counterLine), locale);
}
}
}
finally {
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);
}
}
public static Map<String,Object> createSubContent(int index, String line, String rootContent, Map<String, ? extends Object> context, DispatchContext dctx) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String subContents = null, check = ",", oldChar = "\"", newChar = "", contentNameInprogress = "", contentName = "", contentId = null;
GenericValue Entity = null;
String errMsg = "", sucMsg= "";
subContents = line.substring(index + 1, line.length());
subContents = subContents.replace(oldChar, newChar);
int size = subContents.length();
try {
for (index = 0; index < size; index++) {//start character in line
boolean contentNameMatch = false;
if (subContents.charAt(index) == check.charAt(0)) {//store data
contentName = contentName + contentNameInprogress;
if (contentName.length()>100) {
contentName = contentName.substring(0,100);
}
List<GenericValue> contents = EntityQuery.use(delegator).from("Content").where("contentName", contentName).orderBy("-contentId").queryList();
if (contents != null) {
Iterator<GenericValue> contentCheck = contents.iterator();
while (contentCheck.hasNext() && contentNameMatch == false) {
GenericValue contentch = contentCheck.next();
if (contentch != null) {
List<GenericValue> contentAssocsChecks = EntityQuery.use(delegator).from("ContentAssoc")
.where("contentId", contentch.get("contentId"), "contentIdTo", rootContent)
.queryList();
if (contentAssocsChecks.size() > 0) {
contentNameMatch = true;
}
}
}
}
contentId = null;
if (contentNameMatch == false) {
//create DataResource
Map<String,Object> data = new HashMap<String, Object>();
data.put("userLogin", userLogin);
String dataResourceId = dispatcher.runSync("createDataResource", data).get("dataResourceId").toString();
//create Content
contentId = delegator.getNextSeqId("Content");
Entity = null;
Entity = delegator.makeValue("Content");
Entity.set("contentId", contentId);
Entity.set("contentName", contentName);
Entity.set("contentTypeId", "DOCUMENT");
Entity.set("dataResourceId", dataResourceId);
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);
//Relation Content
Map<String,Object> contentAssoc = new HashMap<String, Object>();
contentAssoc.put("contentId", contentId);
contentAssoc.put("contentAssocTypeId", "SUB_CONTENT");
contentAssoc.put("contentIdTo", rootContent);
contentAssoc.put("userLogin", userLogin);
dispatcher.runSync("createContentAssoc", contentAssoc);
}
contentName ="";
contentNameInprogress="";
}
if ((subContents.charAt(index) )!= check.charAt(0)) {
contentNameInprogress = contentNameInprogress.concat(Character.toString(subContents.charAt(index)));
if (contentNameInprogress.length() > 99) {
contentName = contentName + contentNameInprogress;
contentNameInprogress = "";
}
}
//lastItem
if (index == size - 1) {
contentNameMatch = false;
List<GenericValue> contents = EntityQuery.use(delegator).from("Content").where("contentName", contentName).queryList();
if (contents != null) {
Iterator<GenericValue> contentCheck = contents.iterator();
while (contentCheck.hasNext() && contentNameMatch == false) {
GenericValue contentch = contentCheck.next();
if (contentch != null) {
long contentAssocCount = EntityQuery.use(delegator).from("ContentAssoc")
.where("contentId", contentch.get("contentId"), "contentIdTo", rootContent)
.queryCount();
if (contentAssocCount > 0) {
contentNameMatch = true;
}
}
}
}
contentId = null;
if (contentNameMatch == false) {
//create DataResource
Map<String,Object> data = new HashMap<String, Object>();
data.put("userLogin", userLogin);
String dataResourceId = dispatcher.runSync("createDataResource",data).get("dataResourceId").toString();
//create Content
contentId = delegator.getNextSeqId("Content");
Entity = null;
Entity = delegator.makeValue("Content");
Entity.set("contentId", contentId);
Entity.set("contentName", contentName);
Entity.set("contentTypeId", "DOCUMENT");
Entity.set("dataResourceId", dataResourceId);
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);
//create ContentAssoc
Map<String,Object> contentAssoc = new HashMap<String, Object>();
contentAssoc.put("contentId", contentId);
contentAssoc.put("contentAssocTypeId", "SUB_CONTENT");
contentAssoc.put("contentIdTo", rootContent);
contentAssoc.put("userLogin", userLogin);
dispatcher.runSync("createContentAssoc", contentAssoc);
}
}
}
return ServiceUtil.returnSuccess(sucMsg);
} catch (GenericEntityException e) {
errMsg = "GenericEntityException "+ UtilMisc.toMap("errMessage", e.toString());
Debug.logError(e, errMsg, module);
e.printStackTrace();
return ServiceUtil.returnError(errMsg);
} catch (GenericServiceException e) {
errMsg = "GenericServiceException"+ UtilMisc.toMap("errMessage", e.toString());
Debug.logError(e, errMsg, module);
e.printStackTrace();
return ServiceUtil.returnError(errMsg);
}
}
}