예제 #1
0
파일: UploadUom.java 프로젝트: adp96/xcomv2
  public Document getUomUpload(YFSEnvironment env, Document inXML) throws Exception {
    /** try-catch block added by Arun Sekhar on 01-Feb-2011 for CENT tool logging * */
    Element UOMElement1 = null;
    try {
      int rSet = 0;
      String finalQuery = null;
      NodeList UOMList = inXML.getElementsByTagName("Uom");
      for (int UOMNo = 0; UOMNo < UOMList.getLength(); UOMNo++) {
        String uniqueSequenceNo = getUniqueSequenceNo(env);

        UOMElement1 = (Element) UOMList.item(UOMNo);
        String Uom = UOMElement1.getAttribute("Uom");
        String UomDescription = UOMElement1.getAttribute("UomDescription");

        Document getUomListInputDoc = YFCDocument.createDocument("Uom").getDocument();
        getUomListInputDoc.getDocumentElement().setAttribute("Uom", Uom);
        getUomListInputDoc.getDocumentElement().setAttribute("OrganizationCode", "DEFAULT");
        env.setApiTemplate("getUomList", getUomListTemplate);
        Document getUomListOutputDoc = api.invoke(env, "getUomList", getUomListInputDoc);
        log.info("The output of getUomList is: " + SCXmlUtil.getString(getUomListInputDoc));
        env.clearApiTemplate("getUomList");
        if (getUomListOutputDoc.getDocumentElement().getElementsByTagName("Uom").getLength() > 0) {
          Element uomElement =
              (Element)
                  getUomListOutputDoc.getDocumentElement().getElementsByTagName("Uom").item(0);
          String uomKey = "";
          uomKey = uomElement.getAttribute("UomKey");
          log.info("The uomkey retrieved is: " + uomKey);
          if (uomKey != null && uomKey.trim().length() > 0) {
            finalQuery =
                "UPDATE YFS_UOM SET UOM_DESCRIPTION = '"
                    + UomDescription
                    + "' WHERE UOM_KEY='"
                    + uomKey
                    + "'";
            log.info("The query for uom is: " + finalQuery);
          }
        } else {

          finalQuery =
              "INSERT INTO YFS_UOM(UOM_KEY,ORGANIZATION_CODE,UOM_TYPE,UOM,UOM_DESCRIPTION,CREATEUSERID,MODIFYUSERID,CREATEPROGID,MODIFYPROGID) VALUES('"
                  + uniqueSequenceNo
                  + "','DEFAULT','QUANTITY','"
                  + Uom
                  + "','"
                  + UomDescription
                  + "','xpxBatchLoadAgent','xpxBatchLoadAgent','xpxBatchLoadAgent','xpxBatchLoadAgent')";
          log.info("The query for uom is: " + finalQuery);
        }
        rSet = fireQuery(env, finalQuery, inXML);
        /*if(rSet!=-1)
        {
        	log.info(UomDescription+"YFS_UOM Record not Inserted in YFS_UOM table");

        }*/

        Document getItemUomMasterListInputDoc =
            YFCDocument.createDocument("ItemUOMMaster").getDocument();
        getItemUomMasterListInputDoc.getDocumentElement().setAttribute("UnitOfMeasure", Uom);
        getItemUomMasterListInputDoc.getDocumentElement().setAttribute("OrganizationCode", "xpedx");
        env.setApiTemplate("getItemUOMMasterList", getItemUomMasterListTemplate);
        Document getItemUomMasterListOutputDoc =
            api.invoke(env, "getItemUOMMasterList", getItemUomMasterListInputDoc);
        log.info(
            "The output of getItemUOMMasterList is: "
                + SCXmlUtil.getString(getItemUomMasterListOutputDoc));
        env.clearApiTemplate("getItemUOMMasterList");
        if (getItemUomMasterListOutputDoc
                .getDocumentElement()
                .getElementsByTagName("ItemUOMMaster")
                .getLength()
            > 0) {
          Element itemUommasterElement =
              (Element)
                  getItemUomMasterListOutputDoc
                      .getDocumentElement()
                      .getElementsByTagName("ItemUOMMaster")
                      .item(0);
          String itemUommasterKey = "";
          itemUommasterKey = itemUommasterElement.getAttribute("ItemUOMMasterKey");
          log.info("The item uom master key is: " + itemUommasterKey);
          if (itemUommasterKey != null && itemUommasterKey.trim().length() > 0) {
            finalQuery =
                "UPDATE YFS_ITEM_UOM_MASTER SET DESCRIPTION = '"
                    + UomDescription
                    + "' WHERE ITEM_UOM_MASTER_KEY='"
                    + itemUommasterKey
                    + "'";
            log.info("The query for item_uom_master is: " + finalQuery);
          }
        } else {
          finalQuery =
              "INSERT INTO YFS_ITEM_UOM_MASTER(ITEM_UOM_MASTER_KEY,ORGANIZATION_CODE,UOM ,UOM_TYPE ,ITEM_GROUP_CODE,DESCRIPTION ,ALLOW_FRACTIONS_IN_CONVERSION,IS_INVENTORY_UOM ,IS_ORDERING_UOM,CREATEUSERID,MODIFYUSERID,CREATEPROGID,MODIFYPROGID)values('"
                  + uniqueSequenceNo
                  + "','xpedx','"
                  + Uom
                  + "','QUANTITY','PROD','"
                  + UomDescription
                  + "','N','Y','Y','xpxBatchLoadAgent','xpxBatchLoadAgent','xpxBatchLoadAgent','xpxBatchLoadAgent')";
          log.info("The query for item_uom_master is: " + finalQuery);
        }
        rSet = fireQuery(env, finalQuery, inXML);
        /*if(rSet!=-1)
        {
        	//log.info(UomDescription+"YFS_ITEM_UOM_MASTER Record not Inserted");
        }*/
      }
    } catch (NullPointerException ne) {
      log.error("------------Failed XML Needs to Catch for Re-Processing XML START ----------");
      log.error(SCXmlUtil.getString(UOMElement1));
      log.error("------------Failed XML Needs to Catch for Re-Processing XML END ----------");
      log.error("NullPointerException: " + ne.getStackTrace());
      prepareErrorObject(ne, XPXLiterals.UOM_B_TRANS_TYPE, XPXLiterals.NE_ERROR_CLASS, env, inXML);
      throw ne;
    } catch (YFSException yfe) {
      log.error("------------Failed XML Needs to Catch for Re-Processing XML START ----------");
      log.error(SCXmlUtil.getString(UOMElement1));
      log.error("------------Failed XML Needs to Catch for Re-Processing XML END ----------");
      log.error("YFSException: " + yfe.getStackTrace());
      prepareErrorObject(
          yfe, XPXLiterals.UOM_B_TRANS_TYPE, XPXLiterals.YFE_ERROR_CLASS, env, inXML);
      throw yfe;
    } catch (Exception e) {
      log.error("------------Failed XML Needs to Catch for Re-Processing XML START ----------");
      log.error(SCXmlUtil.getString(UOMElement1));
      log.error("------------Failed XML Needs to Catch for Re-Processing XML END ----------");
      log.error("Exception: " + e.getStackTrace());
      prepareErrorObject(e, XPXLiterals.UOM_B_TRANS_TYPE, XPXLiterals.E_ERROR_CLASS, env, inXML);
      throw e;
    }
    return inXML;
  }
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out = response.getWriter();
    String id = request.getParameter("hashID");
    JSONObject result = new JSONObject();

    if (id != null && id.trim().isEmpty()) {
      response.setContentType("text/plain");
      response.setStatus(400);
      out.println("Empty hash ID");
      return;
    }

    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    String password;
    try {
      // Read the SQL password from a file
      BufferedReader reader = null;
      try {
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("SQLpw.txt");
        reader = new BufferedReader(new InputStreamReader(inputStream));
        password = reader.readLine();
      } catch (NullPointerException e) {
        e.getStackTrace();
        password = "";
      }

      // create a mysql database connection
      String myDriver = "com.mysql.jdbc.Driver";
      String myUrl = "jdbc:mysql://localhost/lodstories";
      Class.forName(myDriver);
      conn = DriverManager.getConnection(myUrl, "root", password);
      st = conn.createStatement();
      rs = st.executeQuery("SELECT hash,title,author FROM hash_objects where id='" + id + "'");

      if (!rs.next()) {
        response.setContentType("text/plain");
        response.setStatus(400);
        out.println("Error retrieving hash object");
        return;
      }

      result.put("hash", rs.getString("hash"));
      result.put("title", rs.getString("title"));
      result.put("author", rs.getString("author"));
      // result.put("path", rs.getString("path"));
      // result.put("rating", rs.getInt("rating"));

      // Update the lastAccessed field
      st.executeUpdate(
          "UPDATE hash_objects SET lastAccessed=CURRENT_TIMESTAMP() WHERE id='" + id + "'");

      response.setContentType("application/json");

      response.setCharacterEncoding("UTF-8");
      out.println(result);
    } catch (ClassNotFoundException e) {
      System.err.println("Could not connect to driver!");
      System.err.println(e.getMessage());

    } catch (SQLException ex) {
      System.err.println(
          "SQLException: "
              + ex.getMessage()
              + ", SQLState: "
              + ex.getSQLState()
              + "VendorError: "
              + ex.getErrorCode());
    } catch (JSONException ex) {
      ex.printStackTrace();
    } finally {
      if (conn != null) {
        try {
          conn.close();
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
      }
      if (st != null) {
        try {
          st.close();
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
      }
      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
      }
    }
  }