Example #1
0
  /**
   * Ensure that the import file is in XML format. If not, we assume it to be a CSV file which is
   * then converted to XML with the specified XSLT script.
   *
   * @param request A model request
   * @param fileName Name of the import file
   * @param xslt XSLT script used to convert to xml
   * @throws ModelException
   */
  protected void convertToXml(ModelRequest request, String fileName, String xslt)
      throws IOException, javax.xml.transform.TransformerConfigurationException,
          javax.xml.transform.TransformerException, ModelException {
    ImportManager im = (ImportManager) request.getSpringBean(ImportManager.ID);

    if (im.validateXmlFile(new File(fileName))) {
      return;
    }

    if (StringTools.isTrimEmpty(xslt)) {
      return;
    }

    File orgFile = new File(fileName);
    File tmpFile = new File(fileName + ".tmp");

    SystemTools.startAndWaitAkteraProcess("/usr/bin/dos2unix", orgFile.getCanonicalPath());

    SystemTools.startAndWaitAkteraProcess(
        "/usr/bin/csv2xml",
        "-r import -d ; -i " + orgFile.getCanonicalPath() + " -o " + tmpFile.getCanonicalPath());

    String xslFileName =
        System.getProperty("keel.config.dir") + "/../resources/csv2xml/" + xslt + ".xsl";

    SystemTools.startAndWaitAkteraProcess(
        "/usr/bin/xsltproc",
        xslFileName + " -o " + orgFile.getCanonicalPath() + " " + tmpFile.getCanonicalPath());

    tmpFile.delete();
  }
Example #2
0
  /**
   * Import a given XML import file. This method generates output elements describing the contents
   * of the import file.
   *
   * @param req The model request.
   * @param res The model response.
   * @param fileName The XML import file.
   * @param handlerId The id of the handler to execute (can be null to execute all handlers).
   * @param xslt XSLT script used to convert to xml.
   * @param destination TODO
   * @return False if the import wasn't started because of an existing lock file.
   */
  protected boolean perform(
      final ModelRequest req,
      ModelResponse res,
      final String fileName,
      final String handlerId,
      String xslt,
      final boolean bulkImport,
      final Properties properties)
      throws ModelException {
    final I18N i18n = (I18N) req.getSpringBean(I18N.ID);
    final ImportManager im = (ImportManager) req.getSpringBean(ImportManager.ID);

    FileTools.newAkteraFile("/var/tmp/iritgo").mkdirs();

    final File lockFile = FileTools.newAkteraFile("/var/tmp/iritgo/import.lck");

    if (lockFile.exists()) {
      return true;
    }

    try {
      lockFile.createNewFile();

      File reportFile = FileTools.newAkteraFile("/var/tmp/iritgo/import-report.txt");

      reportFile.delete();
      reportFile.createNewFile();

      final PrintWriter reporter = new PrintWriter(new FileOutputStream(reportFile), true);

      convertToXml(req, fileName, xslt);

      if (im.validateXmlFile(new File(fileName))) {
        new Thread() {
          public void run() {
            boolean ok = true;
            File file = null;

            try {
              file = new File(fileName);

              Document doc =
                  DocumentBuilderFactory.newInstance()
                      .newDocumentBuilder()
                      .parse("file://" + file.getAbsolutePath());

              XPath xPath = XPathFactory.newInstance().newXPath();

              Node importElem = (Node) xPath.evaluate("import", doc, XPathConstants.NODE);

              reporter.println(i18n.msg(req, "Aktera", "startingImport"));

              ok =
                  im.performImport(
                      req, doc, importElem, reporter, i18n, handlerId, bulkImport, properties);
            } catch (ParserConfigurationException x) {
              reporter.println(i18n.msg(req, "Aktera", "importError", x.toString()));
            } catch (SAXException x) {
              reporter.println(i18n.msg(req, "Aktera", "importError", x.toString()));
            } catch (IOException x) {
              reporter.println(i18n.msg(req, "Aktera", "importError", x.toString()));
            } catch (XPathExpressionException x) {
              reporter.println(i18n.msg(req, "Aktera", "importError", x.toString()));
            } catch (ModelException x) {
              reporter.println(i18n.msg(req, "Aktera", "importError", x.toString()));
            } finally {
              lockFile.delete();
              file.delete();
            }

            reporter.println(i18n.msg(req, "Aktera", "finishedImport"));
            reporter.println();
            reporter.println(i18n.msg(req, "Aktera", "reportFileResult", (ok ? "OK" : "ERROR")));

            reporter.close();
          }
        }.start();
      } else {
        reporter.println(i18n.msg(req, "Aktera", "importFileDoesntContainImportElement"));
        reporter.println();
        reporter.println(i18n.msg(req, "Aktera", "reportFileResult", "ERROR"));
        reporter.close();
        lockFile.delete();
      }
    } catch (Exception x) {
      try {
        lockFile.delete();

        File reportFile = FileTools.newAkteraFile("/var/tmp/iritgo/import-report.txt");
        PrintWriter out = new PrintWriter(reportFile);

        out.println(i18n.msg(req, "Aktera", "importError", x.toString()));
        out.println();
        out.println(i18n.msg(req, "Aktera", "reportFileResult", "ERROR"));
        out.close();
      } catch (IOException xx) {
      }
    }

    return true;
  }