001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: /*
014: * Created on Apr 24, 2005
015: *
016: * TODO To change the template for this generated file go to
017: * Window - Preferences - Java - Code Style - Code Templates
018: */
019: package org.pentaho.repository.filebased.solution;
020:
021: import java.io.File;
022: import java.text.SimpleDateFormat;
023: import java.util.ArrayList;
024: import java.util.Date;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import org.dom4j.Document;
029: import org.dom4j.Element;
030: import org.dom4j.Node;
031: import org.pentaho.core.repository.SolutionRepositoryBase;
032: import org.pentaho.messages.Messages;
033: import org.pentaho.util.logging.ILogger;
034:
035: /**
036: * @author James Dixon
037: *
038: * TODO To change the template for this generated type comment go to Window -
039: * Preferences - Java - Code Style - Code Templates
040: */
041: public class FileInfo {
042:
043: public static final String FILE_TYPE_ACTIVITY = "FILE.ACTIVITY"; //$NON-NLS-1$
044:
045: public static final String FILE_TYPE_FOLDER = "FILE.FOLDER"; //$NON-NLS-1$
046:
047: public static final String FILE_TYPE_RULES = "FILE_RULES"; //$NON-NLS-1$
048:
049: public static final String FILE_TYPE_REPORT = "FILE_REPORT"; //$NON-NLS-1$
050:
051: public static final String FILE_TYPE_WORKFLOW = "FILE_WORKFLOW"; //$NON-NLS-1$
052:
053: public static final String FILE_TYPE_XPDL = "FILE_XPDL"; //$NON-NLS-1$
054:
055: public static final String FILE_TYPE_BIRT = "FILE_BIRT"; //$NON-NLS-1$
056:
057: public static final String FILE_TYPE_MODEL = "FILE_MODEL"; //$NON-NLS-1$
058:
059: public static final String FILE_TYPE_VIEW = "FILE_VIEW"; //$NON-NLS-1$
060:
061: public static final String FILE_TYPE_CONTENT = "FILE_CONTENT"; //$NON-NLS-1$
062:
063: public static final String FILE_TYPE_XML = "FILE_XML"; //$NON-NLS-1$
064:
065: public static final String FILE_TYPE_INDEX = "FILE_INDEX"; //$NON-NLS-1$
066:
067: public static final String FILE_TYPE_URL = "FILE.URL"; //$NON-NLS-1$
068:
069: public static final String FILE_DISPLAY_TYPE_SOLUTION = "solution"; //$NON-NLS-1$
070:
071: public static final String FILE_DISPLAY_TYPE_FOLDER = "folder"; //$NON-NLS-1$
072:
073: public static final String FILE_DISPLAY_TYPE_REPORT = "report"; //$NON-NLS-1$
074:
075: public static final String FILE_DISPLAY_TYPE_PROCESS = "process"; //$NON-NLS-1$
076:
077: public static final String FILE_DISPLAY_TYPE_RULE = "rule"; //$NON-NLS-1$
078:
079: public static final String FILE_DISPLAY_TYPE_VIEW = "view"; //$NON-NLS-1$
080:
081: public static final String FILE_DISPLAY_TYPE_URL = "url"; //$NON-NLS-1$
082:
083: public static final String FILE_DISPLAY_TYPE_UNKNOWN = "unknown"; //$NON-NLS-1$
084:
085: private String author;
086:
087: private String fileName;
088:
089: private String solutionId;
090:
091: private String path;
092:
093: private String name;
094:
095: private String description;
096:
097: private boolean hasParameters;
098:
099: private Date lastUpdated;
100:
101: private long size;
102:
103: private List parameterNames;
104:
105: private String type;
106:
107: private String mimeType;
108:
109: private String iconPath;
110:
111: private String url;
112:
113: private String displayType;
114:
115: private boolean visible;
116:
117: public FileInfo() {
118: }
119:
120: public FileInfo(File file, String path, String solutionId,
121: SolutionRepository repository) {
122:
123: fileName = file.getName();
124: this .path = path;
125: this .solutionId = solutionId;
126: String fileNameCaseless = fileName.toLowerCase();
127: hasParameters = false;
128: iconPath = null;
129: url = null;
130: displayType = FILE_DISPLAY_TYPE_UNKNOWN;
131:
132: if (fileNameCaseless.endsWith(".xaction")) { //$NON-NLS-1$
133: // this is dynamic content - open the document to get the
134: // descriptions
135: Document doc = repository.getSolutionDocument(solutionId,
136: path, fileName);
137: if (doc == null)
138: if (doc != null) {
139: type = FILE_TYPE_ACTIVITY;
140: mimeType = "text/xml"; //$NON-NLS-1$
141: name = doc
142: .selectSingleNode(
143: "/pentaho-activity/activity-info/name").getText(); //$NON-NLS-1$
144: description = doc
145: .selectSingleNode(
146: "/pentaho-activity/activity-info/description").getText(); //$NON-NLS-1$
147: author = doc
148: .selectSingleNode(
149: "/pentaho-activity/activity-info/author").getText(); //$NON-NLS-1$
150: Node node = doc
151: .selectSingleNode("/pentaho-activity/activity-info/display-type"); //$NON-NLS-1$
152: if (node != null) {
153: displayType = node.getText();
154: if (!displayType
155: .equals(FILE_DISPLAY_TYPE_PROCESS)
156: && !displayType
157: .equals(FILE_DISPLAY_TYPE_REPORT)
158: && !displayType
159: .equals(FILE_DISPLAY_TYPE_RULE)
160: && !displayType
161: .equals(FILE_DISPLAY_TYPE_VIEW)) {
162: displayType = FILE_DISPLAY_TYPE_UNKNOWN;
163: }
164: }
165: node = doc
166: .selectSingleNode("/pentaho-activity/activity-info/visible"); //$NON-NLS-1$
167: if (node != null) {
168: visible = "true".equalsIgnoreCase(node.getText()); //$NON-NLS-1$
169: } else {
170: visible = false;
171: }
172: node = doc
173: .selectSingleNode("/pentaho-activity/activity-info/result-mime-type"); //$NON-NLS-1$
174: if (node != null) {
175: mimeType = node.getText();
176:
177: }
178: lastUpdated = null;
179: size = -1;
180: // TODO: read parameters from first activity in file
181: List params = doc
182: .selectNodes("/pentaho-activity/activity-definition[1]/parameters/parameter/name"); //$NON-NLS-1$
183: if (params != null) {
184: Iterator it = params.iterator();
185: parameterNames = new ArrayList();
186: while (it.hasNext()) {
187: parameterNames.add(((Element) it.next())
188: .getText());
189: hasParameters = true;
190: }
191: }
192: }
193: } else if (fileNameCaseless.endsWith(".xml")) { //$NON-NLS-1$
194: visible = false;
195: // see if this is a pentaho document
196: if (fileNameCaseless.endsWith("rules.xml")) { //$NON-NLS-1$
197: type = FILE_TYPE_RULES;
198: } else if (fileNameCaseless.endsWith("birt.xml")) { //$NON-NLS-1$
199: type = FILE_TYPE_BIRT;
200: visible = true;
201: } else if (fileNameCaseless.endsWith("report.xml")) { //$NON-NLS-1$
202: type = FILE_TYPE_REPORT;
203: } else if (fileNameCaseless.endsWith("workflow.xml")) { //$NON-NLS-1$
204: type = FILE_TYPE_WORKFLOW;
205: } else if (fileNameCaseless.endsWith("view.xml")) { //$NON-NLS-1$
206: type = FILE_TYPE_VIEW;
207: } else if (fileNameCaseless.endsWith("model.xml")) { //$NON-NLS-1$
208: type = FILE_TYPE_MODEL;
209: } else if (fileNameCaseless
210: .endsWith(SolutionRepositoryBase.INDEX_FILENAME)) {
211: type = FILE_TYPE_INDEX;
212: } else {
213: type = FILE_TYPE_XML;
214: }
215:
216: mimeType = "text/xml"; //$NON-NLS-1$
217: Document doc = repository.getSolutionDocument(solutionId,
218: path, fileName);
219: if (doc != null) {
220: Node node = doc.selectSingleNode("//file-info/name"); //$NON-NLS-1$
221: if (node == null) {
222: name = fileName.replace('_', ' ');
223: } else {
224: name = node.getText();
225: }
226: node = doc.selectSingleNode("//file-info/description"); //$NON-NLS-1$
227: if (node == null) {
228: description = ""; //$NON-NLS-1$
229: } else {
230: description = node.getText();
231: }
232: node = doc.selectSingleNode("//file-info/author"); //$NON-NLS-1$
233: if (node == null) {
234: author = ""; //$NON-NLS-1$
235: } else {
236: author = node.getText();
237: }
238: node = doc.selectSingleNode("//file-info/icon"); //$NON-NLS-1$
239: if (node == null) {
240: iconPath = ""; //$NON-NLS-1$
241: } else {
242: iconPath = node.getText();
243: }
244: node = doc.selectSingleNode("//file-info/url"); //$NON-NLS-1$
245: if (node == null) {
246: url = ""; //$NON-NLS-1$
247: } else {
248: url = node.getText();
249: type = FILE_TYPE_URL;
250: }
251: node = doc.selectSingleNode("//file-info/visible"); //$NON-NLS-1$
252: if (node != null) {
253: visible = "true".equalsIgnoreCase(node.getText()); //$NON-NLS-1$
254: }
255: node = doc
256: .selectSingleNode("//file-info/result-mime-type"); //$NON-NLS-1$
257: if (node != null) {
258: mimeType = node.getText();
259: }
260: node = doc.selectSingleNode("//file-info/display-type"); //$NON-NLS-1$
261: if (node != null) {
262: displayType = node.getText();
263: if (!displayType.equals(FILE_DISPLAY_TYPE_PROCESS)
264: && !displayType
265: .equals(FILE_DISPLAY_TYPE_REPORT)
266: && !displayType
267: .equals(FILE_DISPLAY_TYPE_RULE)
268: && !displayType
269: .equals(FILE_DISPLAY_TYPE_VIEW)) {
270: displayType = FILE_DISPLAY_TYPE_UNKNOWN;
271: }
272: }
273:
274: lastUpdated = null;
275: size = -1;
276: // TODO: read parameters from first activity in file
277: List params = doc
278: .selectNodes("//parameters/parameter/name"); //$NON-NLS-1$
279: if (params != null) {
280: Iterator it = params.iterator();
281: parameterNames = new ArrayList();
282: while (it.hasNext()) {
283: parameterNames.add(((Element) it.next())
284: .getText());
285: hasParameters = true;
286: }
287: }
288:
289: }
290: } else {
291: // this is static content
292: if (fileNameCaseless.endsWith(".xpdl")) { //$NON-NLS-1$
293: mimeType = "text/xml"; //$NON-NLS-1$
294: type = FILE_TYPE_XPDL;
295: visible = false;
296: } else if (fileNameCaseless.endsWith(".pdf")) { //$NON-NLS-1$
297: mimeType = "application/pdf"; //$NON-NLS-1$
298: type = FILE_TYPE_CONTENT;
299: visible = true;
300: } else if (fileNameCaseless.endsWith(".html")) { //$NON-NLS-1$
301: mimeType = "text/html"; //$NON-NLS-1$
302: type = FILE_TYPE_CONTENT;
303: visible = true;
304: } else if (fileNameCaseless.endsWith(".htm")) { //$NON-NLS-1$
305: mimeType = "text/html"; //$NON-NLS-1$
306: type = FILE_TYPE_CONTENT;
307: visible = true;
308: } else if (fileNameCaseless.endsWith(".xhtml")) { //$NON-NLS-1$
309: mimeType = "text/html"; // until browser support for xhtml improves should be "text/xhtml+xml" //$NON-NLS-1$
310: type = FILE_TYPE_CONTENT;
311: visible = true;
312: }
313: name = fileName.replace('_', ' ');
314: author = ""; //$NON-NLS-1$
315: description = ""; //$NON-NLS-1$
316: hasParameters = false;
317: lastUpdated = new Date(file.lastModified());
318: size = -1;
319: parameterNames = null;
320: }
321:
322: }
323:
324: public FileInfo(Element node, ILogger logger) {
325: String fileType = node.attributeValue("type"); //$NON-NLS-1$
326: if (fileType == null) {
327: // we don't know what to do with this
328: logger
329: .error(Messages
330: .getErrorString("FileInfo.ERROR_0001_DOCUMENT_HAS_NO_TYPE")); //$NON-NLS-1$
331: } else if (node.attributeValue("type").equals(FILE_TYPE_FOLDER)) { //$NON-NLS-1$
332: initFolderFromNode(node, logger);
333: } else {
334: initFileInfoFromNode(node, logger);
335: }
336: }
337:
338: private void initFileInfoFromNode(Element node, ILogger logger) {
339:
340: try {
341: type = node.attributeValue("type"); //$NON-NLS-1$
342: mimeType = node.attributeValue("mimetype"); //$NON-NLS-1$
343: displayType = node.attributeValue("displaytype"); //$NON-NLS-1$
344: visible = "true".equalsIgnoreCase(node.attributeValue("visible")); //$NON-NLS-1$ //$NON-NLS-2$
345: Node tmpNode = node.selectSingleNode("filename"); //$NON-NLS-1$
346: if (tmpNode != null) {
347: fileName = tmpNode.getText();
348: }
349: tmpNode = node.selectSingleNode("solution"); //$NON-NLS-1$
350: if (tmpNode != null) {
351: solutionId = tmpNode.getText();
352: }
353: tmpNode = node.selectSingleNode("path"); //$NON-NLS-1$
354: if (tmpNode != null) {
355: path = tmpNode.getText();
356: }
357: tmpNode = node.selectSingleNode("name"); //$NON-NLS-1$
358: if (tmpNode != null) {
359: name = tmpNode.getText();
360: }
361: tmpNode = node.selectSingleNode("description"); //$NON-NLS-1$
362: if (tmpNode != null) {
363: description = tmpNode.getText();
364: }
365: tmpNode = node.selectSingleNode("has-parameters"); //$NON-NLS-1$
366: if (tmpNode != null) {
367: hasParameters = "true".equals(tmpNode.getText()); //$NON-NLS-1$
368: }
369: tmpNode = node.selectSingleNode("last-modified"); //$NON-NLS-1$
370: if (tmpNode != null) {
371: SimpleDateFormat format = new SimpleDateFormat(
372: "yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
373: try {
374: lastUpdated = format.parse(tmpNode.getText());
375: } catch (Exception e) {
376: lastUpdated = null;
377: }
378: }
379: size = -1;
380: tmpNode = node.selectSingleNode("size"); //$NON-NLS-1$
381: if (tmpNode != null) {
382: size = new Long(tmpNode.getText()).longValue();
383: }
384: tmpNode = node.selectSingleNode("author"); //$NON-NLS-1$
385: if (tmpNode != null) {
386: author = tmpNode.getText();
387: }
388: tmpNode = node.selectSingleNode("icon"); //$NON-NLS-1$
389: if (tmpNode != null) {
390: iconPath = tmpNode.getText();
391: }
392: tmpNode = node.selectSingleNode("url"); //$NON-NLS-1$
393: if (tmpNode != null) {
394: url = tmpNode.getText();
395: }
396: List parameterList = node
397: .selectNodes("parameters/parameter/name"); //$NON-NLS-1$
398: hasParameters = false;
399: if (parameterList != null) {
400: parameterNames = new ArrayList();
401: Iterator it = parameterList.iterator();
402: while (it.hasNext()) {
403: parameterNames.add(((Node) it.next()).getText());
404: hasParameters = true;
405: }
406: } else {
407: parameterNames = null;
408: }
409: } catch (Exception e) {
410: logger
411: .error(
412: Messages
413: .getErrorString("FileInfo.ERROR_0002_COULD_NOT_LOAD"), e); //$NON-NLS-1$
414: }
415: }
416:
417: private void initFolderFromNode(Element node, ILogger logger) {
418: try {
419: type = FILE_TYPE_FOLDER;
420: path = node.selectSingleNode("path").getText(); //$NON-NLS-1$
421: name = node.selectSingleNode("name").getText(); //$NON-NLS-1$
422: description = node
423: .selectSingleNode("description").getText(); //$NON-NLS-1$
424: iconPath = node.selectSingleNode("icon").getText(); //$NON-NLS-1$
425: visible = "true".equalsIgnoreCase(node.selectSingleNode("@visible").getText()); //$NON-NLS-1$ //$NON-NLS-2$
426: solutionId = node.selectSingleNode("solution").getText(); //$NON-NLS-1$
427: } catch (Exception e) {
428: logger
429: .error(
430: Messages
431: .getErrorString("FileInfo.ERROR_0002_COULD_NOT_LOAD"), e); //$NON-NLS-1$
432: }
433: }
434:
435: public Element toXmlNode(Element parent) {
436: Element node = parent.addElement("file"); //$NON-NLS-1$
437: if (type != null) {
438: node.addAttribute("type", type); //$NON-NLS-1$
439: }
440: if (path != null) {
441: node.addElement("path").setText(path); //$NON-NLS-1$
442: }
443: if (name != null) {
444: node.addElement("name").setText(name); //$NON-NLS-1$
445: }
446: if (mimeType != null) {
447: node.addAttribute("mimetype", mimeType); //$NON-NLS-1$
448: }
449: if (displayType != null) {
450: node.addAttribute("displaytype", displayType); //$NON-NLS-1$
451: }
452: node.addAttribute("visible", Boolean.toString(visible)); //$NON-NLS-1$
453: if (author != null) {
454: node.addElement("author").setText(author); //$NON-NLS-1$
455: }
456: if (fileName != null) {
457: node.addElement("filename").setText(fileName); //$NON-NLS-1$
458: }
459: if (solutionId != null) {
460: node.addElement("solution").setText(solutionId); //$NON-NLS-1$
461: }
462: if (url != null) {
463: node.addElement("url").setText(url); //$NON-NLS-1$
464: }
465: if (size != -1) {
466: node.addElement("size").setText(new Long(size).toString()); //$NON-NLS-1$
467: }
468: if (description != null) {
469: node.addElement("description").setText(description); //$NON-NLS-1$
470: }
471: if (iconPath != null) {
472: node.addElement("icon").setText(iconPath); //$NON-NLS-1$
473: }
474: if (lastUpdated == null) {
475: node.addElement("last-modified").setText(""); //$NON-NLS-1$ //$NON-NLS-2$
476: } else {
477: SimpleDateFormat format = new SimpleDateFormat(
478: "yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
479: node
480: .addElement("last-updated").setText(format.format(lastUpdated)); //$NON-NLS-1$
481: }
482: if (parameterNames != null) {
483: node.addElement("has-parameters").setText("true"); //$NON-NLS-1$ //$NON-NLS-2$
484: Element parametersNode = node.addElement("parameters"); //$NON-NLS-1$
485: for (int idx = 0; idx < parameterNames.size(); idx++) {
486: parametersNode
487: .addElement("parameter").addElement("name").setText((String) parameterNames.get(idx)); //$NON-NLS-1$ //$NON-NLS-2$
488: }
489: } else {
490: node.addElement("has-parameters").setText("false"); //$NON-NLS-1$ //$NON-NLS-2$
491: }
492: return node;
493: }
494:
495: public String getAuthor() {
496: return author;
497: }
498:
499: public Date getLastUpdated() {
500: return lastUpdated;
501: }
502:
503: public long getSize() {
504: return size;
505: }
506:
507: public boolean getHasParameters() {
508: return hasParameters;
509: }
510:
511: public String getName() {
512: return name;
513: }
514:
515: public String getUrl() {
516: return url;
517: }
518:
519: public String getFileName() {
520: return fileName;
521: }
522:
523: public String getPath() {
524: return path;
525: }
526:
527: public String getSolutionId() {
528: return solutionId;
529: }
530:
531: public String getDescription() {
532: return description;
533: }
534:
535: public List getParamterNames() {
536: return parameterNames;
537: }
538:
539: public void setAuthor(String author) {
540: this .author = author;
541: }
542:
543: public void setLastUpdated(Date lastUpdated) {
544: this .lastUpdated = lastUpdated;
545: }
546:
547: public void setSize(long size) {
548: this .size = size;
549: }
550:
551: public void setHasParameters(boolean hasParameters) {
552: this .hasParameters = hasParameters;
553: }
554:
555: public void setName(String name) {
556: this .name = name;
557: }
558:
559: public void setUrl(String url) {
560: this .url = url;
561: }
562:
563: public void setDescription(String description) {
564: this .description = description;
565: }
566:
567: public void setParamterNames(List parameterNames) {
568: this.parameterNames = parameterNames;
569: }
570:
571: }
|