コード例 #1
0
  public String getXMLDataForItemId(String itemId) {
    String xmlstore = "";

    Connection conn = null;

    // Create a connection to the database
    try { // System.out.print("before connection");
      // Connection to db manager
      conn = DbManager.getConnection(true);
      Statement statement = conn.createStatement();
      // System.out.print("successfully connected to database");

      // Geting the items
      ResultSet result =
          statement.executeQuery("SELECT * FROM Items " + "WHERE Items.Item_ID = " + itemId);

      result.first();
      // Somethings in it

      if (result.getRow() != 0) {
        // System.out.println("I found a item " );
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        DocumentBuilder b = fac.newDocumentBuilder();
        org.w3c.dom.Document doc = b.newDocument();

        // root element
        Element root = doc.createElement("Item");
        root.setAttribute("ItemID", itemId);
        doc.appendChild(root);

        Element element_name = doc.createElement("Name");
        element_name.appendChild(doc.createTextNode(replacespecial(result.getString("Item_name"))));
        root.appendChild(element_name);

        /*         // Build Category Elements
        // Get the Categories
        Statement catstatement = conn.createStatement();
        ResultSet catresult = catstatement.executeQuery("SELECT Category "
                                                      + "FROM Category,Item_Category "
                                                      + "WHERE Item_Category.Item_ID = " + itemId + " "
                                                      + "AND Item_Category.Category_ID = Category.Category_ID");

        Element category_element;
        while (catresult.next()) {
            category_element = doc.createElement("Category");
            category_element.appendChild(doc.createTextNode(replacespecial(catresult.getString("Category"))));
            root.appendChild(category_element);
        }

        catresult.close();
        catstatement.close();*/

        // Build Item price elements
        if (result.getString("Currently") != null) {
          Element currently_element = doc.createElement("Currently");
          currently_element.appendChild(doc.createTextNode("$" + result.getString("Currently")));
          root.appendChild(currently_element);
        }

        if (result.getString("Buy_Price") != null) {
          Element buyprice_element = doc.createElement("Buy_Price");
          buyprice_element.appendChild(doc.createTextNode("$" + result.getString("Buy_Price")));
          root.appendChild(buyprice_element);
        }

        if (result.getString("First_Bid") != null) {
          Element start_element = doc.createElement("First_Bid");
          start_element.appendChild(doc.createTextNode("$" + result.getString("First_Bid")));
          root.appendChild(start_element);
        }

        // num bids
        if (result.getString("Number_of_Bids") != null) {
          Element numberbids_elements = doc.createElement("Number_of_Bids");
          numberbids_elements.appendChild(doc.createTextNode(result.getString("Number_of_Bids")));
          root.appendChild(numberbids_elements);
        }
        // description

        if (result.getString("Description") != null) {
          Element description_element = doc.createElement("Description");
          description_element.appendChild(
              doc.createTextNode(replacespecial(result.getString("Description"))));
          root.appendChild(description_element);
        }

        /*  // location
        Element location_element = doc.createElement("Location");
        location_element.appendChild(doc.createTextNode((replacespecial(result.getString("Location")))));
        root.appendChild(location_element);

        // country
        Element country_element = doc.createElement("Country");
        country_element.appendChild(doc.createTextNode((replacespecial(result.getString("Country")))));
        root.appendChild(country_element);

        // started
        Element started_elem = doc.createElement("Started");
        started_elem.appendChild(doc.createTextNode(convertDate(result.getString("Started"), "yyyy-MM-dd HH:mm:ss", "MMM-dd-yy HH:mm:ss")));
        root.appendChild(started_elem);

        // ends
        Element ends_element = doc.createElement("Ends");
        ends_element.appendChild(doc.createTextNode(convertDate(result.getString("Ends"), "yyyy-MM-dd HH:mm:ss", "MMM-dd-yy HH:mm:ss")));
        root.appendChild(ends_element);*/

        /*                // Build Bid Elements
        Statement bidstatement = conn.createStatement();
        ResultSet bidresult = bidstatement.executeQuery("SELECT * "
                                                      + "FROM Auctions, AuctionUser "
                                                      + "WHERE Auctions.Item_Id = " + itemId + " "
                                                      + "AND Auctions.User_ID = AuctionUser.User_ID");

        Element bids_element = doc.createElement("Bids");

        while (bidresult.next()) {
            try {
                Element bid_element = doc.createElement("Bid");
                Element bidder_element = doc.createElement("Bidder");
                bidder_element.setAttribute("UserID", replacespecial(bidresult.getString("User_ID")));
               // bidder_element.setAttribute("Rating", bidresult.getString("Rating"));

                // Add Location and Country elements if they aren't NULL
                if (!bidresult.getString("Location").equals("")) {
                    Element location_element = doc.createElement("Location");
                    location_element.appendChild(doc.createTextNode((replacespecial(bidresult.getString("Location")))));
                    bidder_element.appendChild(location_element);
                }
                if (!bidresult.getString("Country").equals("")) {
                    Element country_element = doc.createElement("Country");
                    country_element.appendChild(doc.createTextNode((replacespecial(bidresult.getString("Country")))));
                    bidder_element.appendChild(country_element);
                }
                bid_element.appendChild(bidder_element);

                // time
                Element time_element = doc.createElement("Time");
                time_element.appendChild(doc.createTextNode(convertDate(bidresult.getString("Bid_Time"), "yyyy-MM-dd HH:mm:ss", "MMM-dd-yy HH:mm:ss")));
                bid_element.appendChild(time_element);

                // amount
                Element amount_element = doc.createElement("Amount");
                amount_element.appendChild(doc.createTextNode(bidresult.getString("Bid_Time")));
                bid_element.appendChild(amount_element);

                bids_element.appendChild(bid_element);
            } catch (SQLException e) {
                System.out.println(e);
            }
        }

        root.appendChild(bids_element);

        bidresult.close();
        bidstatement.close();*/

        /*                // Get the Seller data
        Statement sellstatement = conn.createStatement();
        ResultSet sellres = sellstatement.executeQuery("SELECT User_ID, Rating, Location, Country "
                                                     + "FROM Items, AuctionUser "
                                                     + "WHERE Items.Item_ID" + " = " + itemId + " "
                                                     + "AND Items.Seller_Id = AuctionUser.User_ID");
        sellres.first();

        // seller
                        Element sellerElem = doc.createElement("Seller");
                        sellerElem.setAttribute("UserID", (replacespecial(sellres.getString("UserID"))));
                        sellerElem.setAttribute("Rating", sellres.getString("Rating"));
                        root.appendChild(sellerElem);
                        sellres.close();
                        sellstatement.close();*/

        // Write the XML
        TransformerFactory newfactory = TransformerFactory.newInstance();
        Transformer transform = newfactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        StringWriter writer = new StringWriter();
        StreamResult res = new StreamResult(writer);
        transform.setOutputProperty(OutputKeys.INDENT, "yes");
        transform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transform.transform(source, res);
        xmlstore = writer.toString();
        xmlstore = replacespecial1(xmlstore);
      }

      result.close();
      statement.close();

      conn.close();

    } catch (SQLException e) {
      System.out.println(e);
    } catch (ParserConfigurationException e) {
      System.out.println("oops");
    } catch (TransformerException e) {
      System.out.println("oops");
    }

    return xmlstore;
  }
コード例 #2
0
  /** updates the XMl with hashcode for the files */
  protected BudgetSubAwards updateXML(
      byte xmlContents[], Map fileMap, BudgetSubAwards budgetSubAwardBean, Budget budget)
      throws Exception {

    javax.xml.parsers.DocumentBuilderFactory domParserFactory =
        javax.xml.parsers.DocumentBuilderFactory.newInstance();
    javax.xml.parsers.DocumentBuilder domParser = domParserFactory.newDocumentBuilder();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlContents);

    org.w3c.dom.Document document = domParser.parse(byteArrayInputStream);
    byteArrayInputStream.close();
    String namespace = null;
    String formName = null;
    if (document != null) {
      Node node;
      Element element = document.getDocumentElement();
      NamedNodeMap map = element.getAttributes();
      String namespaceHolder =
          element.getNodeName().substring(0, element.getNodeName().indexOf(':'));
      node = map.getNamedItem("xmlns:" + namespaceHolder);
      namespace = node.getNodeValue();
      FormMappingInfo formMappingInfo = new FormMappingLoader().getFormInfo(namespace);
      formName = formMappingInfo.getFormName();
      budgetSubAwardBean.setNamespace(namespace);
      budgetSubAwardBean.setFormName(formName);
    }

    String xpathEmptyNodes =
        "//*[not(node()) and local-name(.) != 'FileLocation' and local-name(.) != 'HashValue']";
    String xpathOtherPers =
        "//*[local-name(.)='ProjectRole' and local-name(../../.)='OtherPersonnel' and count(../NumberOfPersonnel)=0]";
    removeAllEmptyNodes(document, xpathEmptyNodes, 0);
    removeAllEmptyNodes(document, xpathOtherPers, 1);
    removeAllEmptyNodes(document, xpathEmptyNodes, 0);
    changeDataTypeForNumberOfOtherPersons(document);

    List<String> fedNonFedSubAwardForms = getFedNonFedSubawardForms();
    NodeList budgetYearList =
        XPathAPI.selectNodeList(document, "//*[local-name(.) = 'BudgetYear']");
    for (int i = 0; i < budgetYearList.getLength(); i++) {
      Node bgtYearNode = budgetYearList.item(i);
      String period = getValue(XPathAPI.selectSingleNode(bgtYearNode, "BudgetPeriod"));
      if (fedNonFedSubAwardForms.contains(namespace)) {
        Element newBudgetYearElement =
            copyElementToName((Element) bgtYearNode, bgtYearNode.getNodeName());
        bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode);
      } else {
        Element newBudgetYearElement =
            copyElementToName((Element) bgtYearNode, bgtYearNode.getNodeName() + period);
        bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode);
      }
    }

    Node oldroot = document.removeChild(document.getDocumentElement());
    Node newroot = document.appendChild(document.createElement("Forms"));
    newroot.appendChild(oldroot);

    org.w3c.dom.NodeList lstFileName = document.getElementsByTagName("att:FileName");
    org.w3c.dom.NodeList lstFileLocation = document.getElementsByTagName("att:FileLocation");
    org.w3c.dom.NodeList lstMimeType = document.getElementsByTagName("att:MimeType");
    org.w3c.dom.NodeList lstHashValue = document.getElementsByTagName("glob:HashValue");

    if ((lstFileName.getLength() != lstFileLocation.getLength())
        || (lstFileLocation.getLength() != lstHashValue.getLength())) {
      //            throw new RuntimeException("Tag occurances mismatch in XML File");
    }

    org.w3c.dom.Node fileNode, hashNode, mimeTypeNode;
    org.w3c.dom.NamedNodeMap fileNodeMap, hashNodeMap;
    String fileName;
    byte fileBytes[];
    String contentId;
    List attachmentList = new ArrayList();

    for (int index = 0; index < lstFileName.getLength(); index++) {
      fileNode = lstFileName.item(index);

      Node fileNameNode = fileNode.getFirstChild();
      fileName = fileNameNode.getNodeValue();

      fileBytes = (byte[]) fileMap.get(fileName);

      if (fileBytes == null) {
        throw new RuntimeException("FileName mismatch in XML and PDF extracted file");
      }
      String hashVal = GrantApplicationHash.computeAttachmentHash(fileBytes);

      hashNode = lstHashValue.item(index);
      hashNodeMap = hashNode.getAttributes();

      Node temp = document.createTextNode(hashVal);
      hashNode.appendChild(temp);

      hashNode = hashNodeMap.getNamedItem("glob:hashAlgorithm");

      hashNode.setNodeValue(S2SConstants.HASH_ALGORITHM);

      fileNode = lstFileLocation.item(index);
      fileNodeMap = fileNode.getAttributes();
      fileNode = fileNodeMap.getNamedItem("att:href");

      contentId = fileNode.getNodeValue();
      String encodedContentId = cleanContentId(contentId);
      fileNode.setNodeValue(encodedContentId);

      mimeTypeNode = lstMimeType.item(0);
      String contentType = mimeTypeNode.getFirstChild().getNodeValue();

      BudgetSubAwardAttachment budgetSubAwardAttachmentBean = new BudgetSubAwardAttachment();
      budgetSubAwardAttachmentBean.setAttachment(fileBytes);
      budgetSubAwardAttachmentBean.setContentId(encodedContentId);

      budgetSubAwardAttachmentBean.setContentType(contentType);
      budgetSubAwardAttachmentBean.setBudgetId(budgetSubAwardBean.getBudgetId());
      budgetSubAwardAttachmentBean.setSubAwardNumber(budgetSubAwardBean.getSubAwardNumber());

      attachmentList.add(budgetSubAwardAttachmentBean);
    }

    budgetSubAwardBean.setBudgetSubAwardAttachments(attachmentList);

    javax.xml.transform.Transformer transformer =
        javax.xml.transform.TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    javax.xml.transform.stream.StreamResult result =
        new javax.xml.transform.stream.StreamResult(bos);
    javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(document);

    transformer.transform(source, result);

    budgetSubAwardBean.setSubAwardXmlFileData(new String(bos.toByteArray()));

    bos.close();

    return budgetSubAwardBean;
  }