1 package com.calipso.xmleditor; 2 3 import org.w3c.dom.Node ; 4 import org.apache.log4j.FileAppender; 5 import org.apache.log4j.PatternLayout; 6 import org.apache.log4j.Logger; 7 import org.apache.log4j.ConsoleAppender; 8 9 import javax.swing.*; 10 import javax.swing.tree.DefaultTreeModel ; 11 import java.awt.*; 12 import java.awt.event.*; 13 import java.io.*; 14 import java.sql.*; 15 import java.util.*; 16 17 import com.calipso.reportgenerator.reportdefinitions.*; 18 import com.calipso.reportgenerator.reportdefinitions.types.ReportDefinitionReportTypeType; 19 import com.calipso.reportgenerator.common.*; 20 import com.calipso.reportgenerator.client.ReportManagerService; 21 import com.calipso.reportgenerator.userinterface.ExtensionFilter; 22 import com.calipso.reportgenerator.common.ShowExceptionMessageDialog; 23 import com.calipso.reportgenerator.common.InfoException; 24 25 32 33 public class XmlEditorUI extends JFrame implements ActionListener{ 34 35 private JButton btAddNode; 36 private JButton btDeleteNode; 37 private JButton btFinish; 38 private XmlEditorPanel pnlXmlEditor; 39 private XmlEditorLoader xmlLoader; 40 private JPanel pnlCenter; 41 private XmlEditorConnectionPane connectionPane; 42 private IReportManager reportManager = null; 43 private String configPath = null; 44 private ReportGeneratorConfiguration reportGeneratorConfiguration; 45 private String reportDefinitionXsdPath; 46 private String reportSourceDefinitionXsdPath; 47 private String reportViewXsdPath; 48 private XmlEditorMenu menuBar; 49 private XmlEditorCache xmlCache; 50 private static final String defaultErrorMsgTitle = "Error"; 51 private File currentXmlFile; 52 private File currentXsdFile; 53 private static Logger logger; 54 private Map typeColumn = new HashMap(); 55 56 public File getCurrentXsdFile() { 57 return currentXsdFile; 58 } 59 60 public void setCurrentXsdFile(File currentXsdFile) { 61 setCurrentXmlFile(null); 62 this.currentXsdFile = currentXsdFile; 63 } 64 65 70 public XmlEditorUI(String path, String args[]) { 71 this.configPath = path; 72 System.out.println("Init"); 73 getLogger().info(new String ("Init")); 74 try{ 75 reportGeneratorConfiguration = new ReportGeneratorConfiguration(ReportGeneratorConfiguration.getConfiguration(configPath)); 76 }catch (Exception e1){ 77 JOptionPane.showMessageDialog(null, e1, "Init error", JOptionPane.ERROR_MESSAGE); 78 System.exit(1); 79 } 80 Image icon = getReportGeneratorConfiguration().getImage("ICON"); 81 if(icon != null) { 82 setIconImage(icon); 83 } 84 initializePaths(); 85 initialize(); 86 try { 87 if ((args.length > 0)&&(args[0] != null)&&(args[0] != "")) { 88 loadFromXml(true,args[0].toString()); 89 } 90 }catch (Exception ee){ 91 ee.printStackTrace(); 92 } 93 } 94 95 98 private void initializePaths() { 99 reportDefinitionXsdPath = reportGeneratorConfiguration.getSourceReportSchemasPath() + "/" + "ReportDefinition.xsd"; 100 reportSourceDefinitionXsdPath = reportGeneratorConfiguration.getSourceReportSchemasPath() + "/" + "ReportSourceDefinition.xsd"; 101 reportViewXsdPath = reportGeneratorConfiguration.getSourceReportSchemasPath() + "/" + "ReportView.xsd"; 102 } 103 104 private JMenuBar getMnuBar() { 105 if(menuBar==null){ 106 menuBar = new XmlEditorMenu(this); 107 } 108 return menuBar; 109 } 110 111 114 private void initialize() { 115 setTitle(); 116 LanguageTraslator.newLocaleFrom(reportGeneratorConfiguration.getLocaleLanguage(),reportGeneratorConfiguration.getCountry(),reportGeneratorConfiguration.getLanguagesPath()); 117 getContentPane().setLayout(new BorderLayout()); 118 setJMenuBar(getMnuBar()); 119 getContentPane().add(getCenterPanel(), BorderLayout.CENTER); 120 getContentPane().add(getSouthPanel(), BorderLayout.SOUTH); 121 addWindowListener(new Win()); 122 } 123 124 private Component getSouthPanel() { 125 JPanel panel = new JPanel(); 126 GridBagLayout bagLayout = new GridBagLayout(); 127 GridBagConstraints constraints = new GridBagConstraints(); 128 panel.setLayout(bagLayout); 129 130 btAddNode = new JButton(LanguageTraslator.traslate("504")); 131 btAddNode.addActionListener(this); 132 panel.add(btAddNode); 133 constraints.weightx = 30; 134 constraints.weighty = 1; 135 constraints.gridx = 0; 136 constraints.gridy = 0; 137 constraints.anchor = GridBagConstraints.EAST; 138 constraints.fill = GridBagConstraints.PAGE_START; 139 bagLayout.setConstraints(btAddNode, constraints); 140 141 btDeleteNode = new JButton(LanguageTraslator.traslate("505")); 142 btDeleteNode.addActionListener(this); 143 panel.add(btDeleteNode); 144 constraints.weightx = 1; 145 constraints.weighty = 1; 146 constraints.gridx = 1; 147 constraints.gridy = 0; 148 constraints.anchor = GridBagConstraints.EAST; 149 constraints.fill = GridBagConstraints.HORIZONTAL; 150 bagLayout.setConstraints(btDeleteNode, constraints); 151 152 btFinish = new JButton(LanguageTraslator.traslate("506")); 153 btFinish.addActionListener(this); 154 panel.add(btFinish); 155 constraints.weightx = 1; 156 constraints.weighty = 1; 157 constraints.gridx = 2; 158 constraints.gridy = 0; 159 constraints.anchor = GridBagConstraints.WEST; 160 constraints.fill = GridBagConstraints.VERTICAL; 161 bagLayout.setConstraints(btDeleteNode, constraints); 162 163 return panel; 164 } 165 166 170 private Component getCenterPanel() { 171 pnlCenter = new JPanel(new GridLayout(1, 1)); 172 return pnlCenter; 173 } 174 175 179 protected void updateUIFrom(XmlEditorTreeDefinition definition) { 180 try{ 181 pnlCenter.removeAll(); 182 pnlXmlEditor = new XmlEditorPanel(definition.getRootDefinition()); 183 pnlCenter.add(pnlXmlEditor); 184 pnlCenter.validate(); 185 }catch (XmlEditorException e){ 186 showException(e); 187 } 188 } 189 190 public void actionPerformed(ActionEvent e) { 191 if(pnlXmlEditor!=null){ 192 if(e.getSource() == btAddNode) { 193 if (currentXsdFile==null){ 194 JOptionPane.showMessageDialog(this, LanguageTraslator.traslate("507")); 195 } else{ 196 pnlXmlEditor.addNode(); 197 } 198 } else if(e.getSource() == btDeleteNode) { 199 pnlXmlEditor.deleteNode(); 200 } else if(e.getSource() == btFinish) { 201 int result = JOptionPane.showConfirmDialog(this, LanguageTraslator.traslate("508"), LanguageTraslator.traslate("509"),JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 202 if(result==JOptionPane.YES_OPTION){ 203 save(true); 204 } 205 System.exit(0); 206 } 207 }else{ 208 if(e.getSource() == btFinish){ 209 System.exit(0); 210 }else{ 211 showException(new XmlEditorException(LanguageTraslator.traslate("510"))); 212 } 213 } 214 } 215 216 public File getCurrentXmlFile() { 217 return currentXmlFile; 218 } 219 220 public void setCurrentXmlFile(File currentXmlFile) { 221 this.currentXmlFile = currentXmlFile; 222 } 223 224 228 protected void openRecent(String name) { 229 try{ 230 File file = new File(name); 231 if(name.substring(name.lastIndexOf(".")+1).equalsIgnoreCase("xsd")){ 232 loadTreeFromFile(name); 233 setCurrentXsdFile(file); 234 }else{ 235 if(currentXmlFile!=null){ 236 int result = JOptionPane.showConfirmDialog(this, LanguageTraslator.traslate("508"), LanguageTraslator.traslate("509"),JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 237 if(result==JOptionPane.YES_OPTION){ 238 save(true); 239 } 240 } 241 if(file.getName().startsWith(XmlEditorDefaultReports.defaultDefinitionTag)){ 242 loadXmlFromSchema(reportDefinitionXsdPath, file); 243 }else if(file.getName().startsWith(XmlEditorDefaultReports.defaultSourceTag)){ 244 loadXmlFromSchema(reportSourceDefinitionXsdPath, file); 245 }else{ 246 loadXml(file); 247 } 248 setCurrentXmlFile(file); 249 } 250 setTitle(); 251 }catch (XmlEditorException e){ 252 showException(e); 253 } 254 } 255 256 260 public void showException(XmlEditorException e) { 261 ShowExceptionMessageDialog.initExceptionDialogMessage(LanguageTraslator.traslate("511"), e); 263 } 264 265 272 private XmlEditorTreeModelNode getModelNodeFrom(Enumeration children, String nodeName, boolean found) { 273 XmlEditorTreeModelNode returnVal = null; 274 while(children.hasMoreElements() && !found) { 275 XmlEditorTreeModelNode current = (XmlEditorTreeModelNode) children.nextElement(); 276 if(current.getUserObject().equals(nodeName)) { 277 return current; 278 } else { 279 returnVal = getModelNodeFrom(current.children(), nodeName, found); 280 if(returnVal != null) { 281 found = true; 282 } 283 } 284 } 285 return returnVal; 286 } 287 288 292 protected void generateReportLayout() { 293 XmlEditorTreeModelNode node = (XmlEditorTreeModelNode)pnlXmlEditor.getTreeModel().getRoot(); 294 String reportType = node.getUserObject().toString(); 295 if(reportType.equalsIgnoreCase("ReportDefinition") || reportType.equalsIgnoreCase("ReportView")){ 296 XmlEditorTreeModelNode current = getModelNodeFrom(node.children(), reportType, false); 297 if(current==null){ 298 current = (XmlEditorTreeModelNode)node.children().nextElement(); 299 } 300 int result = JOptionPane.showConfirmDialog(this, LanguageTraslator.traslate("512"),LanguageTraslator.traslate("513"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); 301 if(result == JOptionPane.YES_OPTION){ 302 try{ 303 Map attrs = getXmlLayoutProperties(current, reportType); 304 String layoutName = XmlEditorDefaultReports.getLayoutId((String )attrs.get("ReportId")); 305 if(((Integer )attrs.get("LayoutPos")).intValue() > 0){ 306 current.getAttributes().set(((Integer )attrs.get("LayoutPos")).intValue(), layoutName); 307 }else{ 308 showException(new XmlEditorException(LanguageTraslator.traslate("514"))); 309 } 310 XmlEditorXmlGenerator.generateFrom(pnlXmlEditor.getTreeModel(), getCurrentXmlFile().getAbsolutePath()); 311 getReportManager().registerDefinitions(); 312 ReportResult reportResult = getReportResultFromCurrent(reportType, attrs.get("ReportDefinitionId").toString(), getCurrentXmlFile().getAbsolutePath()); 313 writeReportLayout(reportResult, attrs.get("ReportId").toString(), attrs.get("ReportDefinitionId").toString()); 314 JOptionPane.showMessageDialog(this, LanguageTraslator.traslate("515")); 315 }catch (Exception e){ 316 showException(new XmlEditorException(LanguageTraslator.traslate("516"),e)); 317 } 318 } 319 }else{ 320 showException(new XmlEditorException(LanguageTraslator.traslate("517"))); 321 } 322 } 323 324 private ReportResult getReportResultFromCurrent(String reportType, String reportDefinitionId, String viewFileName) throws XmlEditorException{ 325 ReportResult result = null; 326 try{ 327 ReportDefinition definition = getReportManager().getReportDefinitionFromID(reportDefinitionId); 328 if(reportType.equalsIgnoreCase("ReportDefinition")){ 329 if(definition.getReportType()!=ReportDefinitionReportTypeType.STATICSQL){ 330 result = getReportManager().ExecReportQuery(reportDefinitionId, new HashMap()); 331 } 332 }else{ 333 if(definition.getReportType()!=ReportDefinitionReportTypeType.STATICSQL){ 334 int reportHandle = getReportManager().PrepareReport(reportDefinitionId); 335 ReportView view = ReportView.unmarshal(new FileReader(viewFileName)); 336 result = getReportManager().ExecReportQuery(reportHandle, view); 337 } 338 } 339 }catch (Exception e){ 340 throw new XmlEditorException(e); 341 } 342 return result; 343 } 344 345 private Map getXmlLayoutProperties(XmlEditorTreeModelNode current, String reportType) throws XmlEditorException { 346 Map result = new HashMap(); 347 int titlePos = -1; 348 int layoutPos = -1; 349 if(reportType.equalsIgnoreCase("ReportDefinition")){ 350 layoutPos = current.getAttributeNames().indexOf("LayoutDesign"); 351 result.put("LayoutPos", new Integer (layoutPos)); 352 titlePos = current.getAttributeNames().indexOf("Title"); 353 String reportId = current.getId(); 354 result.put("ReportId", reportId); 355 result.put("ReportDefinitionId", reportId); 356 }else if(reportType.equalsIgnoreCase("ReportView")){ 357 layoutPos = current.getAttributeNames().indexOf("ReportLayout"); 358 result.put("LayoutPos", new Integer (layoutPos)); 359 titlePos = current.getAttributeNames().indexOf("Description"); 360 result.put("ReportId", current.getId()); 361 int reportDefinitionIdPos = current.getAttributeNames().indexOf("ReportDefinitionId"); 362 result.put("ReportDefinitionId", current.getAttributes().elementAt(reportDefinitionIdPos)); 363 } 364 try{ 365 result.put("LayoutName", current.getAttributes().elementAt(layoutPos)); 366 }catch (Exception e){ 367 throw new XmlEditorException(LanguageTraslator.traslate("514")); 368 } 369 result.put("Title", current.getAttributes().elementAt(titlePos)); 370 return result; 371 } 372 373 378 public void getParametersFromPane() { 379 XmlEditorConnectionPane connectionPane = getConnectionPane(); 380 connectionPane.pack(); 381 connectionPane.setVisible(true); 382 if(!connectionPane.isCancelled()){ 383 createReportDefinitionsFromSql( connectionPane.getReportName(), connectionPane.getClassName(), connectionPane.getLocalUrl(), 384 connectionPane.getUser(), String.valueOf(connectionPane.getPassword()), connectionPane.getSqlText()); 385 } 386 } 387 388 private XmlEditorConnectionPane getConnectionPane() { 389 if(connectionPane==null){ 390 connectionPane = new XmlEditorConnectionPane(this, true, reportGeneratorConfiguration.getValues()); 391 } 392 return connectionPane; 393 } 394 395 400 private void loadTreeFromFile(String schema) throws XmlEditorException{ 401 XmlEditorTreeDefinition definition = new XmlEditorTreeDefinition(schema); 402 updateUIFrom(definition); 403 } 404 405 408 protected void loadFromXsd() { 409 try{ 410 JFileChooser fileChooser = getFileChooser("xsd", "Schema"); 411 int result = fileChooser.showOpenDialog(this); 412 if(result == JFileChooser.APPROVE_OPTION) { 413 String schemaLocation = fileChooser.getSelectedFile().getPath(); 414 menuBar.addXsdRecentItem(schemaLocation); 415 loadTreeFromFile(schemaLocation); 416 pnlXmlEditor.setOnlyXsd(true); 417 setCurrentXsdFile(fileChooser.getSelectedFile()); 418 setTitle(); 419 } 420 }catch (XmlEditorException e){ 421 showException(e); 422 } 423 } 424 425 431 private JFileChooser getFileChooser(String extension, String description){ 432 JFileChooser fileChooser; 433 if (extension == "xml"){ 434 fileChooser = new JFileChooser(getReportGeneratorConfiguration().getSourceReportDefinitionsPath()); 435 } else if (extension=="xsd"){ 436 fileChooser = new JFileChooser(getReportGeneratorConfiguration().getSourceReportSchemasPath()); 437 } else{ 438 fileChooser = new JFileChooser("."); 439 } 440 fileChooser.setMultiSelectionEnabled(false); 441 fileChooser.setFileFilter(new ExtensionFilter(extension, description)); 442 return fileChooser; 443 } 444 445 452 private void loadXmlFromSchema(String schemaPath, File file) throws XmlEditorException{ 453 loadTreeFromFile(schemaPath); 454 xmlLoader = new XmlEditorLoader(pnlXmlEditor.getTreeModel(), pnlXmlEditor.getNodeDefinition()); 455 DefaultTreeModel newModel = xmlLoader.loadFrom(file, true); 456 pnlXmlEditor.getTree().setModel(newModel); 457 pnlXmlEditor.getTree().repaint(); 458 pnlXmlEditor.setOnlyXsd(false); 459 } 460 461 465 private void loadXml(File file){ 466 try{ 467 xmlLoader = new XmlEditorLoader(); 468 DefaultTreeModel newModel = xmlLoader.loadFrom(file, false); 469 updateXmlPanelFrom(newModel, xmlLoader.getDomRoot()); 470 setCurrentXmlFile(file); 471 }catch (XmlEditorException e){ 472 showException(e); 473 } 474 } 475 476 481 protected void loadFromXml(boolean justXml, String fileName) { 482 File file = null; 483 try{ 484 if (fileName.equals("")) { 485 JFileChooser fileChooser = getFileChooser("xml", "Xml file"); 486 int result = fileChooser.showOpenDialog(this); 487 if (result == JFileChooser.APPROVE_OPTION) { 488 file = fileChooser.getSelectedFile(); 489 } 490 } else { 491 file = new File(fileName); 492 } 493 if (file != null) { 494 menuBar.addXmlRecentItem(file.getAbsolutePath()); 495 if(!justXml) { 496 unloadCurrentXmlFromXsd(); 497 if(pnlXmlEditor==null){ 498 throw new XmlEditorException(LanguageTraslator.traslate("518")); 499 } 500 xmlLoader = new XmlEditorLoader(pnlXmlEditor.getTreeModel(), pnlXmlEditor.getNodeDefinition()); 501 DefaultTreeModel newModel = xmlLoader.loadFrom(file, true); 502 pnlXmlEditor.getTree().setModel(newModel); 503 pnlXmlEditor.getTree().repaint(); 504 pnlXmlEditor.setOnlyXsd(false); 505 } else { 506 loadXml(file); 507 setCurrentXsdFile(null); 508 } 509 setCurrentXmlFile(file); 510 setTitle(); 511 } 512 }catch (XmlEditorException e){ 513 showException(e); 514 } 515 } 516 517 522 private void unloadCurrentXmlFromXsd() throws XmlEditorException{ 523 File currentXsdFile = getCurrentXsdFile(); 524 unLoadxsd(); 525 loadTreeFromFile(currentXsdFile.getAbsolutePath()); 526 setCurrentXsdFile(currentXsdFile); 527 } 528 529 532 private void collapseAllNodes() { 533 pnlXmlEditor.collapseAllNodes(); 534 } 535 536 541 private void updateXmlPanelFrom(DefaultTreeModel newModel, Node domRoot) { 542 if(pnlXmlEditor != null) { 543 pnlXmlEditor.removeAll(); 544 } 545 pnlCenter.removeAll(); 546 pnlXmlEditor = new XmlEditorPanel(newModel, domRoot); 547 pnlCenter.add(pnlXmlEditor); 548 pnlCenter.validate(); 549 } 550 551 563 public void createReportDefinitionsFromSql(String reportName, String className, String localUrl, String user, 564 String password, String sql){ 565 Map currentColumnsDimensions; 566 Map currentColumnsMetrics; 567 Map map2; 568 569 try{ 570 connectionPane.setVisible(false); 571 DataTypeTableFrame dataTypeTableFrame; 572 Class.forName(className); 573 getLogger().info(new String (LanguageTraslator.traslate("519"))); 574 System.out.print(LanguageTraslator.traslate("519")); 575 Connection con = DriverManager.getConnection(localUrl, user, password); 576 Statement stmt = con.createStatement(); 577 582 ResultSet rs = stmt.executeQuery(sql); 583 ResultSetMetaData meta = rs.getMetaData(); 584 585 getLogger().info(new String (LanguageTraslator.traslate("520"))); 586 System.out.println(LanguageTraslator.traslate("520")); 587 currentColumnsDimensions = getColumnsDimensionsMap(meta); 588 currentColumnsMetrics = getColumnsMetricsMap(meta); 589 dataTypeTableFrame = new DataTypeTableFrame(this,typeColumn); 590 dataTypeTableFrame.setVisible(true); 591 if(!dataTypeTableFrame.isCancelled()) { 592 map2 = dataTypeTableFrame.getRowsLatestVersion(); 593 dataTypeTableFrame.setVisible(false); 594 currentColumnsDimensions = getColumnsDimensionsMap(map2); 595 currentColumnsMetrics = getColumnsMetricsMap(map2); 596 } 597 598 writeReports(reportName, currentColumnsDimensions, currentColumnsMetrics, sql, className, localUrl, user, password); 599 File xmlFile = new File(reportGeneratorConfiguration.getSourceReportDefinitionsPath() + "/" + XmlEditorDefaultReports.getReportId(reportName) + ".xml"); 600 setCurrentXmlFile(xmlFile); 601 loadXmlFromSchema(reportDefinitionXsdPath, xmlFile); 602 setTitle(); 603 }catch (Exception e){ 604 showException(new XmlEditorException(e)); 605 } 606 } 607 608 609 620 private void writeReports(String reportName, Map dimensionMap, Map metricMap, String query, String className, String localUrl, String user, String password) { 621 getLogger().info(new String (LanguageTraslator.traslate("521"))); 622 System.out.println(LanguageTraslator.traslate("521")); 623 String reportPath = writeReportDefinition(reportName, dimensionMap, metricMap); 624 System.out.println("reportName:"+reportName+"dimensionMap: "+dimensionMap.size()+ "tostring:"+dimensionMap.toString()+"metricMap:"+metricMap.toString()); 625 menuBar.addXmlRecentItem(reportPath); 626 getLogger().info(new String (LanguageTraslator.traslate("522"))); 627 System.out.println(LanguageTraslator.traslate("522")); 628 reportPath = writeReportSourceDefinition(reportName, dimensionMap, metricMap, query, className, localUrl, user, password); 629 menuBar.addXmlRecentItem(reportPath); 630 } 631 632 639 private String writeReportLayout(ReportResult result,String reportName, String definitionId) throws XmlEditorException{ 640 String layoutName = ""; 641 try{ 642 ReportDefinition definition = getReportManager().getReportDefinitionFromID(definitionId); 643 ReportSpec reportSpec = getReportManager().getReportSpec(definitionId); 644 ReportLayoutBuilder builder = new ReportLayoutBuilder(getReportGeneratorConfiguration(), result, reportSpec); 645 647 IJasperDefinition jasper = builder.buildDefaulJasperDefinition(); 650 if(reportName.equalsIgnoreCase("")){ 652 reportName = definition.getTitle() != null ? definition.getTitle() : definitionId; 653 } 654 layoutName = XmlEditorDefaultReports.getLayoutId(reportName) + ".xml"; 655 new XMLWriter(jasper.getJasperDefinition(true)).saveDocument(reportGeneratorConfiguration.getSourceReportLayoutPath() + "/" + layoutName); 656 }catch (Exception e){ 657 throw new XmlEditorException(e); 658 } 659 return layoutName; 660 } 661 662 private IJasperDefinition getJasperDefinition(ReportResult result) throws XmlEditorException, InfoException { 663 IJasperDefinition jasperDefinition = null; 664 int groupingDimensionCount = result.getReportTableModel().getGroupingDimCount(); 665 int nonGroupingDimensionCount = result.getReportTableModel().getNonGroupingDimCount(); 666 int accumMetricCount = result.getReportTableModel().getAccMetricsCount(); 667 int metricCount = result.getReportTableModel().getCommonMetricsCount(); 668 if(result.getReportSpec().getReportType().getType() == ReportDefinitionReportTypeType.CUBE_TYPE){ 669 jasperDefinition = new CubeJasperReportDefinition(result, result.getReportTableModel().getModel(), groupingDimensionCount, metricCount, nonGroupingDimensionCount, accumMetricCount, result.getReportSpec().getTitle()); 670 }else if (result.getReportSpec().getReportType().getType() == ReportDefinitionReportTypeType.ACCUM_TYPE){ 671 jasperDefinition = new StaticJasperReportDefinition(result, result.getReportTableModel().getModel(), groupingDimensionCount, metricCount, nonGroupingDimensionCount, accumMetricCount, result.getReportSpec().getTitle()); 672 }else if (result.getReportSpec().getReportType().getType() == ReportDefinitionReportTypeType.STATICSQL_TYPE){ 673 throw new XmlEditorException(LanguageTraslator.traslate("523")); 674 677 } 678 jasperDefinition = addStyle(jasperDefinition); 679 return jasperDefinition; 680 } 681 682 private IJasperDefinition addStyle(IJasperDefinition design) { 683 String style = reportGeneratorConfiguration.getReportLayoutStyle(); 684 if(style==null || style.equalsIgnoreCase("")){ 685 return design; 686 }else if(style.equalsIgnoreCase("Calipso")){ 687 return new CalipsoDecoratedReportLayout(design); 688 } 689 return design; 690 } 691 692 public IReportManager getReportManager() { 693 if (reportManager == null) { 694 try { 695 reportManager = ReportManagerService.getReportManagerService("",null, ""); 696 } 697 catch (Exception e) { 698 showException(new XmlEditorException(e)); 699 System.exit(3); 700 } 701 } 702 return reportManager; 703 } 704 705 717 private String writeReportSourceDefinition(String reportName, Map dimensionMap, Map metricMap, String query, String className, String localUrl, String user, String password) { 718 ReportSourceDefinition report = XmlEditorDefaultReports.getReportSourceDefinition(reportName, dimensionMap, metricMap, query, className, localUrl, user, password); 719 String fullPath = reportGeneratorConfiguration.getSourceReportSourceDefinitionsPath() + "/" + XmlEditorDefaultReports.getSourceId(reportName) + ".xml"; 720 try{ 721 getLogger().info(new String (LanguageTraslator.traslate("524"))); 722 System.out.println(LanguageTraslator.traslate("524")); 723 Writer wr = new FileWriter(fullPath); 724 report.marshal(wr); 725 wr.flush(); 726 wr.close(); 727 }catch (Exception e){ 728 showException(new XmlEditorException(e)); 729 } 731 return fullPath; 732 } 733 734 741 private String writeReportDefinition(String reportName, Map dimensionMap, Map metricMap) { 742 ReportDefinition report = XmlEditorDefaultReports.getReportDefinition(reportGeneratorConfiguration, reportName, dimensionMap, metricMap); 743 System.out.println("report.toString():"+report.toString()); 744 System.out.println("report.toString():"+report.getDimensionDefinitions().getDimensionDefinition().length+":"+ report.getDimensionDefinitions().enumerateDimensionDefinition().toString()); 745 String fullPath = reportGeneratorConfiguration.getSourceReportDefinitionsPath() + "/" + XmlEditorDefaultReports.getReportId(reportName) + ".xml"; 746 try{ 747 getLogger().info(new String (LanguageTraslator.traslate("525"))); 748 System.out.println(LanguageTraslator.traslate("525")); 749 Writer wr = new FileWriter(fullPath); 750 report.marshal(wr); 751 wr.flush(); 752 wr.close(); 753 }catch (Exception e){ 754 e.printStackTrace(); 755 showException(new XmlEditorException(e)); 756 } 757 return fullPath; 758 } 759 760 763 public void save(boolean overrideFile) { 764 try{ 765 pnlXmlEditor.valueChanged(); 766 if(pnlXmlEditor!= null){ 767 if ((getCurrentXmlFile() == null)||((!overrideFile)&&(getCurrentXmlFile() != null))){ 768 JFileChooser fileChooser = getFileChooser("xml", "Xml file"); 769 int result = fileChooser.showSaveDialog(this); 770 if(result == JFileChooser.APPROVE_OPTION) { 771 File file = fileChooser.getSelectedFile(); 772 XmlEditorXmlGenerator.generateFrom(pnlXmlEditor.getTreeModel(), file.getAbsolutePath()); 773 menuBar.addXmlRecentItem(file.getAbsolutePath()); 774 } 775 }else{ 776 XmlEditorXmlGenerator.generateFrom(pnlXmlEditor.getTreeModel(), currentXmlFile.getAbsolutePath()); 777 menuBar.addXmlRecentItem(currentXmlFile.getAbsolutePath()); 778 } 779 } 780 }catch (XmlEditorException e){ 781 showException(e); 782 } 783 } 784 785 786 class Win extends WindowAdapter { 787 public void windowClosing( WindowEvent event ) { 788 System.exit(0); 789 } 790 } 791 792 public void setTitle(){ 793 794 String title = "Calipso XML Editor"; 795 if (currentXsdFile!=null){ 796 title = title + " - XSD : "+currentXsdFile.getName(); 797 } 798 if (currentXmlFile!=null){ 799 title = title + " - XML : "+currentXmlFile.getName(); 800 } 801 setTitle(title); 802 } 803 804 public ReportGeneratorConfiguration getReportGeneratorConfiguration() { 805 return reportGeneratorConfiguration; 806 } 807 808 public void unLoadxsd() { 809 pnlCenter.removeAll(); 810 pnlXmlEditor.removeAll(); 811 repaint(); 812 setCurrentXsdFile(null); 813 setTitle(); 814 } 815 816 public static Logger getLogger(){ 817 if (logger == null){ 818 PatternLayout patternLayout = new PatternLayout(); 819 try { 820 ConsoleAppender consoleAppender = new ConsoleAppender(); 821 FileAppender fileAppender = new FileAppender(patternLayout,"xmleditorlog.txt",true); 822 logger = Logger.getLogger("testlogxmleditor"); 823 logger.addAppender(consoleAppender); 824 } catch (IOException e) { 825 e.printStackTrace(); } 827 } 828 return logger; 829 } 830 831 public void generateReportView() { 832 XmlEditorTreeModelNode node = (XmlEditorTreeModelNode)pnlXmlEditor.getTreeModel().getRoot(); 833 String reportType = node.getUserObject().toString(); 834 if(!reportType.equalsIgnoreCase("ReportDefinition")){ 835 showException(new XmlEditorException(LanguageTraslator.traslate("527"))); 836 } 837 XmlEditorTreeModelNode dimensionsGroup = this.getModelNodeFrom(node.children(), "DimensionDefinitions", false); 838 XmlEditorTreeModelNode dimensions = this.getModelNodeFrom(dimensionsGroup.children(), "DimensionDefinition", false); 839 XmlEditorTreeModelNode metricsGroup = this.getModelNodeFrom(node.children(), "MetricDefinitions", false); 840 XmlEditorTreeModelNode metrics = this.getModelNodeFrom(metricsGroup.children(), "MetricDefinition", false); 841 String reportDefinitionId = ""; 842 XmlEditorTreeModelNode mainNode = this.getModelNodeFrom(node.children(), "ReportDefinition", false); 843 if(mainNode != null){ 844 reportDefinitionId = mainNode.getId(); 845 }else{ 846 reportDefinitionId = getReportDefintionId(node); 847 } 848 String reportPath = writeReportView(dimensions, metrics, reportDefinitionId); 849 menuBar.addXmlRecentItem(reportPath); 850 File xmlFile = new File(reportPath); 851 setCurrentXmlFile(xmlFile); 852 try{ 853 loadXmlFromSchema(reportViewXsdPath, xmlFile); 854 }catch (XmlEditorException e){ 855 showException(e); 856 } 857 setTitle(); 858 } 859 860 private String writeReportView(XmlEditorTreeModelNode dimensions, XmlEditorTreeModelNode metrics, String reportDefinitionId) { 861 ReportView reportView = XmlEditorDefaultReports.getReportView(dimensions, metrics, reportDefinitionId); 862 String fullPath = reportGeneratorConfiguration.getSourceReportViewsPath() + "/" + reportView.getId() + ".xml"; 864 try{ 865 getLogger().info(new String (LanguageTraslator.traslate("528"))); 866 System.out.println(LanguageTraslator.traslate("528")); 867 Writer wr = new FileWriter(fullPath); 868 reportView.marshal(wr); 869 wr.flush(); 870 wr.close(); 871 }catch (Exception e){ 872 e.printStackTrace(); 873 showException(new XmlEditorException(e)); 874 } 875 return fullPath; 876 } 877 878 private String getReportDefintionId(XmlEditorTreeModelNode node) { 879 Collection reportDefinitionElementNames = Arrays.asList(new String []{"ActionDefinitions", "DimensionDefinitions", 880 "DrillDownDefinitions", "MetricDefinitions", "FilterDefinitions", "ParameterValues", 881 "ReportDefinition", "Localizations", "Roles"}); 882 Enumeration enumeration = node.children(); 883 while (enumeration.hasMoreElements()) { 884 XmlEditorTreeModelNode child = (XmlEditorTreeModelNode) enumeration.nextElement(); 885 if(!reportDefinitionElementNames.contains(child.getUserObject().toString())){ 886 return child.getId(); 887 } 888 } 889 return ""; 890 } 891 private Map getColumnsDimensionsMap(ResultSetMetaData meta){ 892 Map result = new HashMap(); 893 int cantidad = 0; 894 895 try{ 896 cantidad = meta.getColumnCount(); 897 for (int i=0; i < cantidad; i++) { 898 if (!(isMetric(meta.getColumnType(i+1)))){ 899 System.out.println(LanguageTraslator.traslate("280") + meta.getColumnName(i+1)); 900 result.put(meta.getColumnName(i+1),new Integer (meta.getColumnType(i+1))); 901 Vector data = new Vector(3); 902 data.add(meta.getColumnName(i+1)); 903 data.add("Dimension"); 904 data.add(new Integer (meta.getColumnType(i+1)));; typeColumn.put(new Integer (i+1),data); 906 } 907 } 908 }catch (Exception e){ 909 showException(new XmlEditorException(e)); 910 } 911 return result; 912 } 913 private Map getColumnsMetricsMap(ResultSetMetaData meta){ 914 Map result = new HashMap(); 915 int cantidad = 0; 916 try{ 917 cantidad = meta.getColumnCount(); 918 for (int i=0; i < cantidad; i++) { 919 if (isMetric(meta.getColumnType(i+1))){ 920 System.out.println(LanguageTraslator.traslate("529") + meta.getColumnName(i+1)); 921 result.put(meta.getColumnName(i+1),new Integer (meta.getColumnType(i+1))); 922 Vector data = new Vector(3); 923 data.add(meta.getColumnName(i+1)); 924 data.add("Metric"); 925 data.add(new Integer (meta.getColumnType(i+1))); typeColumn.put(new Integer (i+1),data); 927 } 928 } 929 }catch (Exception e){ 930 showException(new XmlEditorException(e)); 931 } 932 return result; 933 } 934 939 940 private boolean isMetric(int typeColumn){ 941 942 switch(typeColumn) { 943 case Types.TINYINT: 944 case Types.SMALLINT: 945 case Types.INTEGER: 946 case Types.BIGINT: 947 case Types.FLOAT: 948 case Types.DOUBLE: 949 case Types.REAL: 950 case Types.DECIMAL: 951 case Types.NUMERIC: 952 return true; 953 default: 954 return false; 955 } 956 } 957 958 private Map getColumnsDimensionsMap(Map map){ 959 Map mapAux = new HashMap(); 960 for (int i = 0; i < (this.typeColumn.keySet().size()); i++) { 961 Vector vector =(Vector) map.get(new Integer (1+i)); 962 if (vector != null){ 963 String columnName; 964 Vector vec = new Vector(2); 965 Integer columnType; 966 String extData; 967 if (vector.get(1).equals("Dimension") ) { 968 columnName = (String ) vector.get(0); 969 columnType = getIdType((String )vector.get(2)); 970 extData = (String )vector.get(3); 971 vec.add(columnType); 972 vec.add(extData); 973 mapAux.put(columnName,vec); 974 } 975 } 976 } 977 return mapAux; 978 } 979 private Integer getIdType(String s){ 980 if(s.equalsIgnoreCase("DECIMAL")){ 981 return new Integer (3); 982 } 983 if(s.equalsIgnoreCase("INTEGER")){ 984 return new Integer (4); 985 } 986 if(s.equalsIgnoreCase("FLOAT")){ 987 return new Integer (6); 988 } 989 if(s.equalsIgnoreCase("BOOLEAN")){ 990 return new Integer (16); 991 } 992 if(s.equalsIgnoreCase("DATETIME")){ 993 return new Integer (91); 994 }else{ 995 return new Integer (12); 996 } 997 } 998 999 private Map getColumnsMetricsMap(Map map){ 1000 Map mapAux = new HashMap(); 1001 for (int i = 0; i < (this.typeColumn.keySet().size()); i++) { 1002 Vector vector =(Vector) map.get(new Integer (1+i)); 1003 if (vector != null){ 1004 String columnName; 1005 Vector vec = new Vector(2); 1006 Integer columnType; 1007 String extData; 1008 if (vector.get(1).equals("Metric") ) { 1009 columnName = (String ) vector.get(0); 1010 columnType = getIdType((String )vector.get(2)); 1011 extData = (String )vector.get(3); 1012 vec.add(columnType); 1013 vec.add(extData); 1014 mapAux.put(columnName,vec); 1015 } 1016 } 1017 } 1018return mapAux; 1019 } 1020} 1021 1022 | Popular Tags |