1 13 19 package org.pentaho.repository.filebased.solution; 20 21 import java.io.File ; 22 import java.text.SimpleDateFormat ; 23 import java.util.ArrayList ; 24 import java.util.Date ; 25 import java.util.Iterator ; 26 import java.util.List ; 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 39 public class FileInfo { 40 41 public static final String FILE_TYPE_ACTIVITY = "FILE.ACTIVITY"; 43 public static final String FILE_TYPE_FOLDER = "FILE.FOLDER"; 45 public static final String FILE_TYPE_RULES = "FILE_RULES"; 47 public static final String FILE_TYPE_REPORT = "FILE_REPORT"; 49 public static final String FILE_TYPE_WORKFLOW = "FILE_WORKFLOW"; 51 public static final String FILE_TYPE_XPDL = "FILE_XPDL"; 53 public static final String FILE_TYPE_BIRT = "FILE_BIRT"; 55 public static final String FILE_TYPE_MODEL = "FILE_MODEL"; 57 public static final String FILE_TYPE_VIEW = "FILE_VIEW"; 59 public static final String FILE_TYPE_CONTENT = "FILE_CONTENT"; 61 public static final String FILE_TYPE_XML = "FILE_XML"; 63 public static final String FILE_TYPE_INDEX = "FILE_INDEX"; 65 public static final String FILE_TYPE_URL = "FILE.URL"; 67 public static final String FILE_DISPLAY_TYPE_SOLUTION = "solution"; 69 public static final String FILE_DISPLAY_TYPE_FOLDER = "folder"; 71 public static final String FILE_DISPLAY_TYPE_REPORT = "report"; 73 public static final String FILE_DISPLAY_TYPE_PROCESS = "process"; 75 public static final String FILE_DISPLAY_TYPE_RULE = "rule"; 77 public static final String FILE_DISPLAY_TYPE_VIEW = "view"; 79 public static final String FILE_DISPLAY_TYPE_URL = "url"; 81 public static final String FILE_DISPLAY_TYPE_UNKNOWN = "unknown"; 83 private String author; 84 85 private String fileName; 86 87 private String solutionId; 88 89 private String path; 90 91 private String name; 92 93 private String description; 94 95 private boolean hasParameters; 96 97 private Date lastUpdated; 98 99 private long size; 100 101 private List parameterNames; 102 103 private String type; 104 105 private String mimeType; 106 107 private String iconPath; 108 109 private String url; 110 111 private String displayType; 112 113 private boolean visible; 114 115 public FileInfo() { 116 } 117 118 public FileInfo(File file, String path, String solutionId, SolutionRepository repository) { 119 120 fileName = file.getName(); 121 this.path = path; 122 this.solutionId = solutionId; 123 String fileNameCaseless = fileName.toLowerCase(); 124 hasParameters = false; 125 iconPath = null; 126 url = null; 127 displayType = FILE_DISPLAY_TYPE_UNKNOWN; 128 129 if (fileNameCaseless.endsWith(".xaction")) { Document doc = repository.getSolutionDocument(solutionId, path, fileName); 133 if (doc == null) 134 if (doc != null) { 135 type = FILE_TYPE_ACTIVITY; 136 mimeType = "text/xml"; name = doc.selectSingleNode("/pentaho-activity/activity-info/name").getText(); description = doc.selectSingleNode("/pentaho-activity/activity-info/description").getText(); author = doc.selectSingleNode("/pentaho-activity/activity-info/author").getText(); Node node = doc.selectSingleNode("/pentaho-activity/activity-info/display-type"); 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"); if (node != null) { 149 visible = "true".equalsIgnoreCase(node.getText()); } else { 151 visible = false; 152 } 153 node = doc.selectSingleNode("/pentaho-activity/activity-info/result-mime-type"); if (node != null) { 155 mimeType = node.getText(); 156 157 } 158 lastUpdated = null; 159 size = -1; 160 List params = doc.selectNodes("/pentaho-activity/activity-definition[1]/parameters/parameter/name"); if (params != null) { 163 Iterator it = params.iterator(); 164 parameterNames = new ArrayList (); 165 while (it.hasNext()) { 166 parameterNames.add(((Element) it.next()).getText()); 167 hasParameters = true; 168 } 169 } 170 } 171 } else if (fileNameCaseless.endsWith(".xml")) { visible = false; 173 if (fileNameCaseless.endsWith("rules.xml")) { type = FILE_TYPE_RULES; 176 } else if (fileNameCaseless.endsWith("birt.xml")) { type = FILE_TYPE_BIRT; 178 visible = true; 179 } else if (fileNameCaseless.endsWith("report.xml")) { type = FILE_TYPE_REPORT; 181 } else if (fileNameCaseless.endsWith("workflow.xml")) { type = FILE_TYPE_WORKFLOW; 183 } else if (fileNameCaseless.endsWith("view.xml")) { type = FILE_TYPE_VIEW; 185 } else if (fileNameCaseless.endsWith("model.xml")) { type = FILE_TYPE_MODEL; 187 } else if (fileNameCaseless.endsWith("index.xml")) { type = FILE_TYPE_INDEX; 189 } else { 190 type = FILE_TYPE_XML; 191 } 192 193 mimeType = "text/xml"; Document doc = repository.getSolutionDocument(solutionId, path, fileName); 195 if (doc != null) { 196 Node node = doc.selectSingleNode("//file-info/name"); if (node == null) { 198 name = fileName.replace('_', ' '); 199 } else { 200 name = node.getText(); 201 } 202 node = doc.selectSingleNode("//file-info/description"); if (node == null) { 204 description = ""; } else { 206 description = node.getText(); 207 } 208 node = doc.selectSingleNode("//file-info/author"); if (node == null) { 210 author = ""; } else { 212 author = node.getText(); 213 } 214 node = doc.selectSingleNode("//file-info/icon"); if (node == null) { 216 iconPath = ""; } else { 218 iconPath = node.getText(); 219 } 220 node = doc.selectSingleNode("//file-info/url"); if (node == null) { 222 url = ""; } else { 224 url = node.getText(); 225 type = FILE_TYPE_URL; 226 } 227 node = doc.selectSingleNode("//file-info/visible"); if (node != null) { 229 visible = "true".equalsIgnoreCase(node.getText()); } 231 node = doc.selectSingleNode("//file-info/result-mime-type"); if (node != null) { 233 mimeType = node.getText(); 234 } 235 node = doc.selectSingleNode("//file-info/display-type"); 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 List params = doc.selectNodes("//parameters/parameter/name"); if (params != null) { 248 Iterator it = params.iterator(); 249 parameterNames = new ArrayList (); 250 while (it.hasNext()) { 251 parameterNames.add(((Element) it.next()).getText()); 252 hasParameters = true; 253 } 254 } 255 256 } 257 } else { 258 if (fileNameCaseless.endsWith(".xpdl")) { mimeType = "text/xml"; type = FILE_TYPE_XPDL; 262 visible = false; 263 } else if (fileNameCaseless.endsWith(".pdf")) { mimeType = "application/pdf"; type = FILE_TYPE_CONTENT; 266 visible = true; 267 } else if (fileNameCaseless.endsWith(".html")) { mimeType = "text/html"; type = FILE_TYPE_CONTENT; 270 visible = true; 271 } else if (fileNameCaseless.endsWith(".htm")) { mimeType = "text/html"; type = FILE_TYPE_CONTENT; 274 visible = true; 275 } else if (fileNameCaseless.endsWith(".xhtml")) { mimeType = "text/html"; type = FILE_TYPE_CONTENT; 278 visible = true; 279 } 280 name = fileName.replace('_', ' '); 281 author = ""; description = ""; hasParameters = false; 284 lastUpdated = new Date (file.lastModified()); 285 size = -1; 286 parameterNames = null; 287 } 288 289 } 290 291 public FileInfo(Element node, ILogger logger) { 292 String fileType = node.attributeValue("type"); if (fileType == null) { 294 logger.error(Messages.getErrorString("FileInfo.ERROR_0001_DOCUMENT_HAS_NO_TYPE")); } else if (node.attributeValue("type").equals(FILE_TYPE_FOLDER)) { 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"); mimeType = node.attributeValue("mimetype"); displayType = node.attributeValue("displaytype"); visible = "true".equalsIgnoreCase(node.attributeValue("visible")); Node tmpNode = node.selectSingleNode("filename"); if (tmpNode != null) { 312 fileName = tmpNode.getText(); 313 } 314 tmpNode = node.selectSingleNode("solution"); if (tmpNode != null) { 316 solutionId = tmpNode.getText(); 317 } 318 tmpNode = node.selectSingleNode("path"); if (tmpNode != null) { 320 path = tmpNode.getText(); 321 } 322 tmpNode = node.selectSingleNode("name"); if (tmpNode != null) { 324 name = tmpNode.getText(); 325 } 326 tmpNode = node.selectSingleNode("description"); if (tmpNode != null) { 328 description = tmpNode.getText(); 329 } 330 tmpNode = node.selectSingleNode("has-parameters"); if (tmpNode != null) { 332 hasParameters = "true".equals(tmpNode.getText()); } 334 tmpNode = node.selectSingleNode("last-modified"); if (tmpNode != null) { 336 SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); try { 338 lastUpdated = format.parse(tmpNode.getText()); 339 } catch (Exception e) { 340 lastUpdated = null; 341 } 342 } 343 size = -1; 344 tmpNode = node.selectSingleNode("size"); if (tmpNode != null) { 346 size = new Long (tmpNode.getText()).longValue(); 347 } 348 tmpNode = node.selectSingleNode("author"); if (tmpNode != null) { 350 author = tmpNode.getText(); 351 } 352 tmpNode = node.selectSingleNode("icon"); if (tmpNode != null) { 354 iconPath = tmpNode.getText(); 355 } 356 tmpNode = node.selectSingleNode("url"); if (tmpNode != null) { 358 url = tmpNode.getText(); 359 } 360 List parameterList = node.selectNodes("parameters/parameter/name"); hasParameters = false; 362 if (parameterList != null) { 363 parameterNames = new ArrayList (); 364 Iterator 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 e) { 373 logger.error(Messages.getErrorString("FileInfo.ERROR_0002_COULD_NOT_LOAD"), e); } 375 } 376 377 private void initFolderFromNode(Element node, ILogger logger) { 378 try { 379 type = FILE_TYPE_FOLDER; 380 path = node.selectSingleNode("path").getText(); name = node.selectSingleNode("name").getText(); description = node.selectSingleNode("description").getText(); iconPath = node.selectSingleNode("icon").getText(); visible = "true".equalsIgnoreCase(node.selectSingleNode("@visible").getText()); solutionId = node.selectSingleNode("solution").getText(); } catch (Exception e) { 387 logger.error(Messages.getErrorString("FileInfo.ERROR_0002_COULD_NOT_LOAD"), e); } 389 } 390 391 public Element toXmlNode(Element parent) { 392 Element node = parent.addElement("file"); if (type != null) { 394 node.addAttribute("type", type); } 396 if (path != null) { 397 node.addElement("path").setText(path); } 399 if (name != null) { 400 node.addElement("name").setText(name); } 402 if (mimeType != null) { 403 node.addAttribute("mimetype", mimeType); } 405 if (displayType != null) { 406 node.addAttribute("displaytype", displayType); } 408 node.addAttribute("visible", Boolean.toString(visible)); if (author != null) { 410 node.addElement("author").setText(author); } 412 if (fileName != null) { 413 node.addElement("filename").setText(fileName); } 415 if (solutionId != null) { 416 node.addElement("solution").setText(solutionId); } 418 if (url != null) { 419 node.addElement("url").setText(url); } 421 if (size != -1) { 422 node.addElement("size").setText(new Long (size).toString()); } 424 if (description != null) { 425 node.addElement("description").setText(description); } 427 if (iconPath != null) { 428 node.addElement("icon").setText(iconPath); } 430 if (lastUpdated == null) { 431 node.addElement("last-modified").setText(""); } else { 433 SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); node.addElement("last-updated").setText(format.format(lastUpdated)); } 436 if (parameterNames != null) { 437 node.addElement("has-parameters").setText("true"); Element parametersNode = node.addElement("parameters"); for (int idx = 0; idx < parameterNames.size(); idx++) { 440 parametersNode.addElement("parameter").addElement("name").setText((String ) parameterNames.get(idx)); } 442 } else { 443 node.addElement("has-parameters").setText("false"); } 445 return node; 446 } 447 448 public String getAuthor() { 449 return author; 450 } 451 452 public Date 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 getName() { 465 return name; 466 } 467 468 public String getUrl() { 469 return url; 470 } 471 472 public String getFileName() { 473 return fileName; 474 } 475 476 public String getPath() { 477 return path; 478 } 479 480 public String getSolutionId() { 481 return solutionId; 482 } 483 484 public String getDescription() { 485 return description; 486 } 487 488 public List getParamterNames() { 489 return parameterNames; 490 } 491 492 public void setAuthor(String author) { 493 this.author = author; 494 } 495 496 public void setLastUpdated(Date 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 name) { 509 this.name = name; 510 } 511 512 public void setUrl(String url) { 513 this.url = url; 514 } 515 516 public void setDescription(String description) { 517 this.description = description; 518 } 519 520 public void setParamterNames(List parameterNames) { 521 this.parameterNames = parameterNames; 522 } 523 524 } 525 | Popular Tags |