KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > repository > filebased > solution > FileInfo


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12 */

13 /*
14  * Created on Apr 24, 2005
15  *
16  * TODO To change the template for this generated file go to
17  * Window - Preferences - Java - Code Style - Code Templates
18  */

19 package org.pentaho.repository.filebased.solution;
20
21 import java.io.File JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import org.dom4j.Document;
28 import org.dom4j.Element;
29 import org.dom4j.Node;
30 import org.pentaho.messages.Messages;
31 import org.pentaho.util.logging.ILogger;
32
33 /**
34  * @author James Dixon
35  *
36  * TODO To change the template for this generated type comment go to Window -
37  * Preferences - Java - Code Style - Code Templates
38  */

39 public class FileInfo {
40
41     public static final String JavaDoc FILE_TYPE_ACTIVITY = "FILE.ACTIVITY"; //$NON-NLS-1$
42

43     public static final String JavaDoc FILE_TYPE_FOLDER = "FILE.FOLDER"; //$NON-NLS-1$
44

45     public static final String JavaDoc FILE_TYPE_RULES = "FILE_RULES"; //$NON-NLS-1$
46

47     public static final String JavaDoc FILE_TYPE_REPORT = "FILE_REPORT"; //$NON-NLS-1$
48

49     public static final String JavaDoc FILE_TYPE_WORKFLOW = "FILE_WORKFLOW"; //$NON-NLS-1$
50

51     public static final String JavaDoc FILE_TYPE_XPDL = "FILE_XPDL"; //$NON-NLS-1$
52

53     public static final String JavaDoc FILE_TYPE_BIRT = "FILE_BIRT"; //$NON-NLS-1$
54

55     public static final String JavaDoc FILE_TYPE_MODEL = "FILE_MODEL"; //$NON-NLS-1$
56

57     public static final String JavaDoc FILE_TYPE_VIEW = "FILE_VIEW"; //$NON-NLS-1$
58

59     public static final String JavaDoc FILE_TYPE_CONTENT = "FILE_CONTENT"; //$NON-NLS-1$
60

61     public static final String JavaDoc FILE_TYPE_XML = "FILE_XML"; //$NON-NLS-1$
62

63     public static final String JavaDoc FILE_TYPE_INDEX = "FILE_INDEX"; //$NON-NLS-1$
64

65     public static final String JavaDoc FILE_TYPE_URL = "FILE.URL"; //$NON-NLS-1$
66

67     public static final String JavaDoc FILE_DISPLAY_TYPE_SOLUTION = "solution"; //$NON-NLS-1$
68

69     public static final String JavaDoc FILE_DISPLAY_TYPE_FOLDER = "folder"; //$NON-NLS-1$
70

71     public static final String JavaDoc FILE_DISPLAY_TYPE_REPORT = "report"; //$NON-NLS-1$
72

73     public static final String JavaDoc FILE_DISPLAY_TYPE_PROCESS = "process"; //$NON-NLS-1$
74

75     public static final String JavaDoc FILE_DISPLAY_TYPE_RULE = "rule"; //$NON-NLS-1$
76

77     public static final String JavaDoc FILE_DISPLAY_TYPE_VIEW = "view"; //$NON-NLS-1$
78

79     public static final String JavaDoc FILE_DISPLAY_TYPE_URL = "url"; //$NON-NLS-1$
80

81     public static final String JavaDoc FILE_DISPLAY_TYPE_UNKNOWN = "unknown"; //$NON-NLS-1$
82

83     private String JavaDoc author;
84
85     private String JavaDoc fileName;
86
87     private String JavaDoc solutionId;
88
89     private String JavaDoc path;
90
91     private String JavaDoc name;
92
93     private String JavaDoc description;
94
95     private boolean hasParameters;
96
97     private Date JavaDoc lastUpdated;
98
99     private long size;
100
101     private List JavaDoc parameterNames;
102
103     private String JavaDoc type;
104
105     private String JavaDoc mimeType;
106
107     private String JavaDoc iconPath;
108
109     private String JavaDoc url;
110
111     private String JavaDoc displayType;
112
113     private boolean visible;
114
115     public FileInfo() {
116     }
117
118     public FileInfo(File JavaDoc file, String JavaDoc path, String JavaDoc solutionId, SolutionRepository repository) {
119
120         fileName = file.getName();
121         this.path = path;
122         this.solutionId = solutionId;
123         String JavaDoc fileNameCaseless = fileName.toLowerCase();
124         hasParameters = false;
125         iconPath = null;
126         url = null;
127         displayType = FILE_DISPLAY_TYPE_UNKNOWN;
128
129         if (fileNameCaseless.endsWith(".xaction")) { //$NON-NLS-1$
130
// this is dynamic content - open the document to get the
131
// descriptions
132
Document doc = repository.getSolutionDocument(solutionId, path, fileName);
133             if (doc == null)
134                 if (doc != null) {
135                     type = FILE_TYPE_ACTIVITY;
136                     mimeType = "text/xml"; //$NON-NLS-1$
137
name = doc.selectSingleNode("/pentaho-activity/activity-info/name").getText(); //$NON-NLS-1$
138
description = doc.selectSingleNode("/pentaho-activity/activity-info/description").getText(); //$NON-NLS-1$
139
author = doc.selectSingleNode("/pentaho-activity/activity-info/author").getText(); //$NON-NLS-1$
140
Node node = doc.selectSingleNode("/pentaho-activity/activity-info/display-type"); //$NON-NLS-1$
141
if (node != null) {
142                         displayType = node.getText();
143                         if (!displayType.equals(FILE_DISPLAY_TYPE_PROCESS) && !displayType.equals(FILE_DISPLAY_TYPE_REPORT) && !displayType.equals(FILE_DISPLAY_TYPE_RULE) && !displayType.equals(FILE_DISPLAY_TYPE_VIEW)) {
144                             displayType = FILE_DISPLAY_TYPE_UNKNOWN;
145                         }
146                     }
147                     node = doc.selectSingleNode("/pentaho-activity/activity-info/visible"); //$NON-NLS-1$
148
if (node != null) {
149                         visible = "true".equalsIgnoreCase(node.getText()); //$NON-NLS-1$
150
} else {
151                         visible = false;
152                     }
153                     node = doc.selectSingleNode("/pentaho-activity/activity-info/result-mime-type"); //$NON-NLS-1$
154
if (node != null) {
155                         mimeType = node.getText();
156
157                     }
158                     lastUpdated = null;
159                     size = -1;
160                     // TODO: read parameters from first activity in file
161
List JavaDoc params = doc.selectNodes("/pentaho-activity/activity-definition[1]/parameters/parameter/name"); //$NON-NLS-1$
162
if (params != null) {
163                         Iterator JavaDoc it = params.iterator();
164                         parameterNames = new ArrayList JavaDoc();
165                         while (it.hasNext()) {
166                             parameterNames.add(((Element) it.next()).getText());
167                             hasParameters = true;
168                         }
169                     }
170                 }
171         } else if (fileNameCaseless.endsWith(".xml")) { //$NON-NLS-1$
172
visible = false;
173             // see if this is a pentaho document
174
if (fileNameCaseless.endsWith("rules.xml")) { //$NON-NLS-1$
175
type = FILE_TYPE_RULES;
176             } else if (fileNameCaseless.endsWith("birt.xml")) { //$NON-NLS-1$
177
type = FILE_TYPE_BIRT;
178                 visible = true;
179             } else if (fileNameCaseless.endsWith("report.xml")) { //$NON-NLS-1$
180
type = FILE_TYPE_REPORT;
181             } else if (fileNameCaseless.endsWith("workflow.xml")) { //$NON-NLS-1$
182
type = FILE_TYPE_WORKFLOW;
183             } else if (fileNameCaseless.endsWith("view.xml")) { //$NON-NLS-1$
184
type = FILE_TYPE_VIEW;
185             } else if (fileNameCaseless.endsWith("model.xml")) { //$NON-NLS-1$
186
type = FILE_TYPE_MODEL;
187             } else if (fileNameCaseless.endsWith("index.xml")) { //$NON-NLS-1$
188
type = FILE_TYPE_INDEX;
189             } else {
190                 type = FILE_TYPE_XML;
191             }
192
193             mimeType = "text/xml"; //$NON-NLS-1$
194
Document doc = repository.getSolutionDocument(solutionId, path, fileName);
195             if (doc != null) {
196                 Node node = doc.selectSingleNode("//file-info/name"); //$NON-NLS-1$
197
if (node == null) {
198                     name = fileName.replace('_', ' ');
199                 } else {
200                     name = node.getText();
201                 }
202                 node = doc.selectSingleNode("//file-info/description"); //$NON-NLS-1$
203
if (node == null) {
204                     description = ""; //$NON-NLS-1$
205
} else {
206                     description = node.getText();
207                 }
208                 node = doc.selectSingleNode("//file-info/author"); //$NON-NLS-1$
209
if (node == null) {
210                     author = ""; //$NON-NLS-1$
211
} else {
212                     author = node.getText();
213                 }
214                 node = doc.selectSingleNode("//file-info/icon"); //$NON-NLS-1$
215
if (node == null) {
216                     iconPath = ""; //$NON-NLS-1$
217
} else {
218                     iconPath = node.getText();
219                 }
220                 node = doc.selectSingleNode("//file-info/url"); //$NON-NLS-1$
221
if (node == null) {
222                     url = ""; //$NON-NLS-1$
223
} else {
224                     url = node.getText();
225                     type = FILE_TYPE_URL;
226                 }
227                 node = doc.selectSingleNode("//file-info/visible"); //$NON-NLS-1$
228
if (node != null) {
229                     visible = "true".equalsIgnoreCase(node.getText()); //$NON-NLS-1$
230
}
231                 node = doc.selectSingleNode("//file-info/result-mime-type"); //$NON-NLS-1$
232
if (node != null) {
233                     mimeType = node.getText();
234                 }
235                 node = doc.selectSingleNode("//file-info/display-type"); //$NON-NLS-1$
236
if (node != null) {
237                     displayType = node.getText();
238                     if (!displayType.equals(FILE_DISPLAY_TYPE_PROCESS) && !displayType.equals(FILE_DISPLAY_TYPE_REPORT) && !displayType.equals(FILE_DISPLAY_TYPE_RULE) && !displayType.equals(FILE_DISPLAY_TYPE_VIEW)) {
239                         displayType = FILE_DISPLAY_TYPE_UNKNOWN;
240                     }
241                 }
242
243                 lastUpdated = null;
244                 size = -1;
245                 // TODO: read parameters from first activity in file
246
List JavaDoc params = doc.selectNodes("//parameters/parameter/name"); //$NON-NLS-1$
247
if (params != null) {
248                     Iterator JavaDoc it = params.iterator();
249                     parameterNames = new ArrayList JavaDoc();
250                     while (it.hasNext()) {
251                         parameterNames.add(((Element) it.next()).getText());
252                         hasParameters = true;
253                     }
254                 }
255
256             }
257         } else {
258             // this is static content
259
if (fileNameCaseless.endsWith(".xpdl")) { //$NON-NLS-1$
260
mimeType = "text/xml"; //$NON-NLS-1$
261
type = FILE_TYPE_XPDL;
262                 visible = false;
263             } else if (fileNameCaseless.endsWith(".pdf")) { //$NON-NLS-1$
264
mimeType = "application/pdf"; //$NON-NLS-1$
265
type = FILE_TYPE_CONTENT;
266                 visible = true;
267             } else if (fileNameCaseless.endsWith(".html")) { //$NON-NLS-1$
268
mimeType = "text/html"; //$NON-NLS-1$
269
type = FILE_TYPE_CONTENT;
270                 visible = true;
271             } else if (fileNameCaseless.endsWith(".htm")) { //$NON-NLS-1$
272
mimeType = "text/html"; //$NON-NLS-1$
273
type = FILE_TYPE_CONTENT;
274                 visible = true;
275             } else if (fileNameCaseless.endsWith(".xhtml")) { //$NON-NLS-1$
276
mimeType = "text/html"; // until browser support for xhtml improves should be "text/xhtml+xml" //$NON-NLS-1$
277
type = FILE_TYPE_CONTENT;
278                 visible = true;
279             }
280             name = fileName.replace('_', ' ');
281             author = ""; //$NON-NLS-1$
282
description = ""; //$NON-NLS-1$
283
hasParameters = false;
284             lastUpdated = new Date JavaDoc(file.lastModified());
285             size = -1;
286             parameterNames = null;
287         }
288
289     }
290
291     public FileInfo(Element node, ILogger logger) {
292         String JavaDoc fileType = node.attributeValue("type"); //$NON-NLS-1$
293
if (fileType == null) {
294             // we don't know what to do with this
295
logger.error(Messages.getErrorString("FileInfo.ERROR_0001_DOCUMENT_HAS_NO_TYPE")); //$NON-NLS-1$
296
} else if (node.attributeValue("type").equals(FILE_TYPE_FOLDER)) { //$NON-NLS-1$
297
initFolderFromNode(node, logger);
298         } else {
299             initFileInfoFromNode(node, logger);
300         }
301     }
302
303     private void initFileInfoFromNode(Element node, ILogger logger) {
304
305         try {
306             type = node.attributeValue("type"); //$NON-NLS-1$
307
mimeType = node.attributeValue("mimetype"); //$NON-NLS-1$
308
displayType = node.attributeValue("displaytype"); //$NON-NLS-1$
309
visible = "true".equalsIgnoreCase(node.attributeValue("visible")); //$NON-NLS-1$ //$NON-NLS-2$
310
Node tmpNode = node.selectSingleNode("filename"); //$NON-NLS-1$
311
if (tmpNode != null) {
312                 fileName = tmpNode.getText();
313             }
314             tmpNode = node.selectSingleNode("solution"); //$NON-NLS-1$
315
if (tmpNode != null) {
316                 solutionId = tmpNode.getText();
317             }
318             tmpNode = node.selectSingleNode("path"); //$NON-NLS-1$
319
if (tmpNode != null) {
320                 path = tmpNode.getText();
321             }
322             tmpNode = node.selectSingleNode("name"); //$NON-NLS-1$
323
if (tmpNode != null) {
324                 name = tmpNode.getText();
325             }
326             tmpNode = node.selectSingleNode("description"); //$NON-NLS-1$
327
if (tmpNode != null) {
328                 description = tmpNode.getText();
329             }
330             tmpNode = node.selectSingleNode("has-parameters"); //$NON-NLS-1$
331
if (tmpNode != null) {
332                 hasParameters = "true".equals(tmpNode.getText()); //$NON-NLS-1$
333
}
334             tmpNode = node.selectSingleNode("last-modified"); //$NON-NLS-1$
335
if (tmpNode != null) {
336                 SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
337
try {
338                     lastUpdated = format.parse(tmpNode.getText());
339                 } catch (Exception JavaDoc e) {
340                     lastUpdated = null;
341                 }
342             }
343             size = -1;
344             tmpNode = node.selectSingleNode("size"); //$NON-NLS-1$
345
if (tmpNode != null) {
346                 size = new Long JavaDoc(tmpNode.getText()).longValue();
347             }
348             tmpNode = node.selectSingleNode("author"); //$NON-NLS-1$
349
if (tmpNode != null) {
350                 author = tmpNode.getText();
351             }
352             tmpNode = node.selectSingleNode("icon"); //$NON-NLS-1$
353
if (tmpNode != null) {
354                 iconPath = tmpNode.getText();
355             }
356             tmpNode = node.selectSingleNode("url"); //$NON-NLS-1$
357
if (tmpNode != null) {
358                 url = tmpNode.getText();
359             }
360             List JavaDoc parameterList = node.selectNodes("parameters/parameter/name"); //$NON-NLS-1$
361
hasParameters = false;
362             if (parameterList != null) {
363                 parameterNames = new ArrayList JavaDoc();
364                 Iterator JavaDoc it = parameterList.iterator();
365                 while (it.hasNext()) {
366                     parameterNames.add(((Node) it.next()).getText());
367                     hasParameters = true;
368                 }
369             } else {
370                 parameterNames = null;
371             }
372         } catch (Exception JavaDoc e) {
373             logger.error(Messages.getErrorString("FileInfo.ERROR_0002_COULD_NOT_LOAD"), e); //$NON-NLS-1$
374
}
375     }
376
377     private void initFolderFromNode(Element node, ILogger logger) {
378         try {
379             type = FILE_TYPE_FOLDER;
380             path = node.selectSingleNode("path").getText(); //$NON-NLS-1$
381
name = node.selectSingleNode("name").getText(); //$NON-NLS-1$
382
description = node.selectSingleNode("description").getText(); //$NON-NLS-1$
383
iconPath = node.selectSingleNode("icon").getText(); //$NON-NLS-1$
384
visible = "true".equalsIgnoreCase(node.selectSingleNode("@visible").getText()); //$NON-NLS-1$ //$NON-NLS-2$
385
solutionId = node.selectSingleNode("solution").getText(); //$NON-NLS-1$
386
} catch (Exception JavaDoc e) {
387             logger.error(Messages.getErrorString("FileInfo.ERROR_0002_COULD_NOT_LOAD"), e); //$NON-NLS-1$
388
}
389     }
390
391     public Element toXmlNode(Element parent) {
392         Element node = parent.addElement("file"); //$NON-NLS-1$
393
if (type != null) {
394             node.addAttribute("type", type); //$NON-NLS-1$
395
}
396         if (path != null) {
397             node.addElement("path").setText(path); //$NON-NLS-1$
398
}
399         if (name != null) {
400             node.addElement("name").setText(name); //$NON-NLS-1$
401
}
402         if (mimeType != null) {
403             node.addAttribute("mimetype", mimeType); //$NON-NLS-1$
404
}
405         if (displayType != null) {
406             node.addAttribute("displaytype", displayType); //$NON-NLS-1$
407
}
408         node.addAttribute("visible", Boolean.toString(visible)); //$NON-NLS-1$
409
if (author != null) {
410             node.addElement("author").setText(author); //$NON-NLS-1$
411
}
412         if (fileName != null) {
413             node.addElement("filename").setText(fileName); //$NON-NLS-1$
414
}
415         if (solutionId != null) {
416             node.addElement("solution").setText(solutionId); //$NON-NLS-1$
417
}
418         if (url != null) {
419             node.addElement("url").setText(url); //$NON-NLS-1$
420
}
421         if (size != -1) {
422             node.addElement("size").setText(new Long JavaDoc(size).toString()); //$NON-NLS-1$
423
}
424         if (description != null) {
425             node.addElement("description").setText(description); //$NON-NLS-1$
426
}
427         if (iconPath != null) {
428             node.addElement("icon").setText(iconPath); //$NON-NLS-1$
429
}
430         if (lastUpdated == null) {
431             node.addElement("last-modified").setText(""); //$NON-NLS-1$ //$NON-NLS-2$
432
} else {
433             SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
434
node.addElement("last-updated").setText(format.format(lastUpdated)); //$NON-NLS-1$
435
}
436         if (parameterNames != null) {
437             node.addElement("has-parameters").setText("true"); //$NON-NLS-1$ //$NON-NLS-2$
438
Element parametersNode = node.addElement("parameters"); //$NON-NLS-1$
439
for (int idx = 0; idx < parameterNames.size(); idx++) {
440                 parametersNode.addElement("parameter").addElement("name").setText((String JavaDoc) parameterNames.get(idx)); //$NON-NLS-1$ //$NON-NLS-2$
441
}
442         } else {
443             node.addElement("has-parameters").setText("false"); //$NON-NLS-1$ //$NON-NLS-2$
444
}
445         return node;
446     }
447
448     public String JavaDoc getAuthor() {
449         return author;
450     }
451
452     public Date JavaDoc getLastUpdated() {
453         return lastUpdated;
454     }
455
456     public long getSize() {
457         return size;
458     }
459
460     public boolean getHasParameters() {
461         return hasParameters;
462     }
463
464     public String JavaDoc getName() {
465         return name;
466     }
467
468     public String JavaDoc getUrl() {
469         return url;
470     }
471
472     public String JavaDoc getFileName() {
473         return fileName;
474     }
475
476     public String JavaDoc getPath() {
477         return path;
478     }
479
480     public String JavaDoc getSolutionId() {
481         return solutionId;
482     }
483
484     public String JavaDoc getDescription() {
485         return description;
486     }
487
488     public List JavaDoc getParamterNames() {
489         return parameterNames;
490     }
491
492     public void setAuthor(String JavaDoc author) {
493         this.author = author;
494     }
495
496     public void setLastUpdated(Date JavaDoc lastUpdated) {
497         this.lastUpdated = lastUpdated;
498     }
499
500     public void setSize(long size) {
501         this.size = size;
502     }
503
504     public void setHasParameters(boolean hasParameters) {
505         this.hasParameters = hasParameters;
506     }
507
508     public void setName(String JavaDoc name) {
509         this.name = name;
510     }
511
512     public void setUrl(String JavaDoc url) {
513         this.url = url;
514     }
515
516     public void setDescription(String JavaDoc description) {
517         this.description = description;
518     }
519
520     public void setParamterNames(List JavaDoc parameterNames) {
521         this.parameterNames = parameterNames;
522     }
523
524 }
525
Popular Tags