コード例 #1
0
  @Override
  public void writeTo(
      QueryResult queryResult,
      Class<?> aClass,
      Type type,
      Annotation[] annotations,
      MediaType mediaType,
      MultivaluedMap<String, Object> multivaluedMap,
      OutputStream outputStream)
      throws IOException, WebApplicationException {

    if (queryResult.getExportType().equals(QueryResult.ExportType.JSON)) {
      try {
        generateJSONResponse(outputStream, queryResult);
      } catch (NamingException e) {
        LOGGER.log(Level.SEVERE, null, e);
      }
    } else if (queryResult.getExportType().equals(QueryResult.ExportType.XLS)) {
      excelGenerator.generateXLSResponse(
          queryResult, new Locale(queryResult.getQuery().getAuthor().getLanguage()), "");
    } else {
      throw new IllegalArgumentException();
    }
  }
コード例 #2
0
  public File generateXLSResponse(QueryResult queryResult, Locale locale, String baseURL) {
    File excelFile = new File("export_parts.xls");
    // Blank workbook
    XSSFWorkbook workbook = new XSSFWorkbook();

    // Create a blank sheet
    XSSFSheet sheet = workbook.createSheet("Parts Data");

    String header = StringUtils.join(queryResult.getQuery().getSelects(), ";");
    String[] columns = header.split(";");

    Map<Integer, String[]> data = new HashMap<>();
    String[] headerFormatted = createXLSHeaderRow(header, columns, locale);
    data.put(1, headerFormatted);

    Map<Integer, String[]> commentsData = new HashMap<>();
    String[] headerComments = createXLSHeaderRowComments(header, columns);
    commentsData.put(1, headerComments);

    List<String> selects = queryResult.getQuery().getSelects();
    int i = 1;
    for (QueryResultRow row : queryResult.getRows()) {
      i++;
      data.put(i, createXLSRow(selects, row, baseURL));
      commentsData.put(i, createXLSRowComments(selects, row));
    }

    // Iterate over data and write to sheet
    Set<Integer> keyset = data.keySet();
    int rownum = 0;

    for (Integer key : keyset) {

      Row row = sheet.createRow(rownum++);
      String[] objArr = data.get(key);
      int cellnum = 0;
      for (String obj : objArr) {
        Cell cell = row.createCell(cellnum++);
        cell.setCellValue(obj);
      }

      CreationHelper factory = workbook.getCreationHelper();
      Drawing drawing = sheet.createDrawingPatriarch();
      String[] commentsObjArr = commentsData.get(key);
      cellnum = 0;
      for (String commentsObj : commentsObjArr) {
        if (commentsObj.length() > 0) {
          Cell cell = row.getCell(cellnum) != null ? row.getCell(cellnum) : row.createCell(cellnum);

          // When the comment box is visible, have it show in a 1x3 space
          ClientAnchor anchor = factory.createClientAnchor();
          anchor.setCol1(cell.getColumnIndex());
          anchor.setCol2(cell.getColumnIndex() + 1);
          anchor.setRow1(row.getRowNum());
          anchor.setRow2(row.getRowNum() + 1);

          Comment comment = drawing.createCellComment(anchor);
          RichTextString str = factory.createRichTextString(commentsObj);
          comment.setString(str);

          // Assign the comment to the cell
          cell.setCellComment(comment);
        }
        cellnum++;
      }
    }

    // Define header style
    Font headerFont = workbook.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    headerFont.setFontHeightInPoints((short) 10);
    headerFont.setFontName("Courier New");
    headerFont.setItalic(true);
    headerFont.setColor(IndexedColors.WHITE.getIndex());
    CellStyle headerStyle = workbook.createCellStyle();
    headerStyle.setFont(headerFont);
    headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

    // Set header style
    for (int j = 0; j < columns.length; j++) {
      Cell cell = sheet.getRow(0).getCell(j);
      cell.setCellStyle(headerStyle);

      if (cell.getCellComment() != null) {
        String comment = cell.getCellComment().getString().toString();

        if (comment.equals(QueryField.CTX_PRODUCT_ID)
            || comment.equals(QueryField.CTX_SERIAL_NUMBER)
            || comment.equals(QueryField.PART_MASTER_NUMBER)) {
          for (int k = 0; k < queryResult.getRows().size(); k++) {
            Cell grayCell =
                sheet.getRow(k + 1).getCell(j) != null
                    ? sheet.getRow(k + 1).getCell(j)
                    : sheet.getRow(k + 1).createCell(j);
            grayCell.setCellStyle(headerStyle);
          }
        }
      }
    }

    try {
      // Write the workbook in file system
      FileOutputStream out = new FileOutputStream(excelFile);
      workbook.write(out);
      out.close();
    } catch (Exception e) {
      LOGGER.log(Level.FINEST, null, e);
    }
    return excelFile;
  }
コード例 #3
0
  private void generateJSONResponse(OutputStream outputStream, QueryResult queryResult)
      throws UnsupportedEncodingException, NamingException {

    String charSet = "UTF-8";
    JsonGenerator jg = Json.createGenerator(new OutputStreamWriter(outputStream, charSet));
    jg.writeStartArray();

    List<String> selects = queryResult.getQuery().getSelects();
    List<String> partIterationSelectedAttributes = getPartIterationSelectedAttributes(selects);
    List<String> pathDataSelectedAttributes = getPathDataSelectedAttributes(selects);

    context = new InitialContext();
    IProductInstanceManagerLocal productInstanceService =
        (IProductInstanceManagerLocal)
            context.lookup(
                "java:global/docdoku-server-ear/docdoku-server-ejb/ProductInstanceManagerBean");

    for (QueryResultRow row : queryResult.getRows()) {

      QueryContext queryContext = row.getContext();

      PartRevision part = row.getPartRevision();
      PartIteration lastCheckedInIteration = part.getLastCheckedInIteration();

      jg.writeStartObject();

      jg.write(QueryField.PART_REVISION_PART_KEY, part.getPartNumber() + '-' + part.getVersion());

      // PartMaster data

      if (selects.contains(QueryField.PART_MASTER_NUMBER)) {
        jg.write(QueryField.PART_MASTER_NUMBER, part.getPartNumber());
      }

      if (selects.contains(QueryField.PART_MASTER_NAME)) {
        String sName = part.getPartName();
        jg.write(QueryField.PART_MASTER_NAME, sName != null ? sName : "");
      }

      if (selects.contains(QueryField.PART_MASTER_TYPE)) {
        String sType = part.getType();
        jg.write(QueryField.PART_MASTER_TYPE, sType != null ? sType : "");
      }

      // PartRevision data

      if (selects.contains(QueryField.PART_REVISION_MODIFICATION_DATE)) {
        PartIteration pi = part.getLastIteration();
        if (pi != null) {
          writeDate(jg, QueryField.PART_REVISION_MODIFICATION_DATE, pi.getModificationDate());
        }
      }

      if (selects.contains(QueryField.PART_REVISION_CREATION_DATE)) {
        writeDate(jg, QueryField.PART_REVISION_CREATION_DATE, part.getCreationDate());
      }

      if (selects.contains(QueryField.PART_REVISION_CHECKOUT_DATE)) {
        writeDate(jg, QueryField.PART_REVISION_CHECKOUT_DATE, part.getCheckOutDate());
      }

      if (selects.contains(QueryField.PART_REVISION_CHECKIN_DATE)) {
        writeDate(
            jg,
            QueryField.PART_REVISION_CHECKIN_DATE,
            lastCheckedInIteration != null ? lastCheckedInIteration.getCheckInDate() : null);
      }

      if (selects.contains(QueryField.PART_REVISION_VERSION)) {
        String version = part.getVersion();
        jg.write(QueryField.PART_REVISION_VERSION, version);
      }

      if (selects.contains(QueryField.PART_REVISION_LIFECYCLE_STATE)) {
        String lifeCycleState = part.getLifeCycleState();
        jg.write(
            QueryField.PART_REVISION_LIFECYCLE_STATE, lifeCycleState != null ? lifeCycleState : "");
      }

      if (selects.contains(QueryField.PART_REVISION_STATUS)) {
        PartRevision.RevisionStatus status = part.getStatus();
        jg.write(QueryField.PART_REVISION_STATUS, status.toString());
      }

      if (selects.contains(QueryField.AUTHOR_LOGIN)) {
        User user = part.getAuthor();
        jg.write(QueryField.AUTHOR_LOGIN, user.getLogin());
      }

      if (selects.contains(QueryField.AUTHOR_NAME)) {
        User user = part.getAuthor();
        jg.write(QueryField.AUTHOR_NAME, user.getName());
      }

      if (selects.contains(QueryField.CTX_DEPTH)) {
        jg.write(QueryField.CTX_DEPTH, row.getDepth());
      }

      if (selects.contains(QueryField.PART_ITERATION_LINKED_DOCUMENTS)) {

        StringBuilder sb = new StringBuilder();

        if (null != queryContext && null != queryContext.getSerialNumber()) {
          try {

            ProductInstanceMaster productInstanceMaster =
                productInstanceService.getProductInstanceMaster(
                    new ProductInstanceMasterKey(
                        queryContext.getSerialNumber(),
                        queryContext.getWorkspaceId(),
                        queryContext.getConfigurationItemId()));
            ProductInstanceIteration lastIteration = productInstanceMaster.getLastIteration();
            ProductBaseline basedOn = lastIteration.getBasedOn();
            PartCollection partCollection = basedOn.getPartCollection();
            BaselinedPart baselinedPart =
                partCollection.getBaselinedPart(
                    new BaselinedPartKey(
                        partCollection.getId(),
                        queryContext.getWorkspaceId(),
                        part.getPartNumber()));
            PartIteration targetPart = baselinedPart.getTargetPart();
            Set<DocumentLink> linkedDocuments = targetPart.getLinkedDocuments();
            DocumentCollection documentCollection = basedOn.getDocumentCollection();

            for (DocumentLink documentLink : linkedDocuments) {
              DocumentRevision targetDocument = documentLink.getTargetDocument();
              BaselinedDocument baselinedDocument =
                  documentCollection.getBaselinedDocument(
                      new BaselinedDocumentKey(
                          documentCollection.getId(),
                          queryContext.getWorkspaceId(),
                          targetDocument.getDocumentMasterId(),
                          targetDocument.getVersion()));
              if (null != baselinedDocument) {
                DocumentIteration targetDocumentIteration = baselinedDocument.getTargetDocument();
                sb.append(targetDocumentIteration.toString() + ",");
              }
            }

          } catch (UserNotFoundException
              | UserNotActiveException
              | WorkspaceNotFoundException
              | ProductInstanceMasterNotFoundException e) {
            LOGGER.log(Level.FINEST, null, e);
          }
        } else {
          if (lastCheckedInIteration != null) {
            Set<DocumentLink> linkedDocuments = lastCheckedInIteration.getLinkedDocuments();

            for (DocumentLink documentLink : linkedDocuments) {
              DocumentRevision targetDocument = documentLink.getTargetDocument();
              DocumentIteration targetDocumentLastCheckedInIteration =
                  targetDocument.getLastCheckedInIteration();
              if (targetDocumentLastCheckedInIteration != null) {
                sb.append(targetDocumentLastCheckedInIteration.toString() + ",");
              }
            }
          }
        }

        jg.write(QueryField.PART_ITERATION_LINKED_DOCUMENTS, sb.toString());
      }

      for (String attributeSelect : partIterationSelectedAttributes) {

        String attributeSelectType =
            attributeSelect
                .substring(0, attributeSelect.indexOf("."))
                .substring(QueryField.PART_REVISION_ATTRIBUTES_PREFIX.length());

        String attributeSelectName = attributeSelect.substring(attributeSelect.indexOf(".") + 1);

        String attributeValue = "";

        PartIteration pi = part.getLastIteration();

        if (pi != null) {
          List<InstanceAttribute> attributes = pi.getInstanceAttributes();

          if (attributes != null) {
            jg.writeStartArray(attributeSelect);

            for (InstanceAttribute attribute : attributes) {
              InstanceAttributeDescriptor attributeDescriptor =
                  new InstanceAttributeDescriptor(attribute);

              if (attributeDescriptor.getName().equals(attributeSelectName)
                  && attributeDescriptor.getStringType().equals(attributeSelectType)) {

                attributeValue = attribute.getValue() + "";

                if (attribute instanceof InstanceDateAttribute) {
                  attributeValue =
                      getFormattedDate(((InstanceDateAttribute) attribute).getDateValue());
                } else if (attribute instanceof InstanceListOfValuesAttribute) {
                  attributeValue = ((InstanceListOfValuesAttribute) attribute).getSelectedName();
                }

                jg.write(attributeValue);
              }
            }

            jg.writeEnd();

          } else {
            jg.write(attributeSelect, attributeValue);
          }

        } else {
          // TODO: maybe this line is useless and should be removed
          jg.write(attributeSelect, attributeValue);
        }
      }

      for (String attributeSelect : pathDataSelectedAttributes) {

        String attributeSelectType =
            attributeSelect
                .substring(0, attributeSelect.indexOf("."))
                .substring(QueryField.PATH_DATA_ATTRIBUTES_PREFIX.length());

        String attributeSelectName = attributeSelect.substring(attributeSelect.indexOf(".") + 1);

        String attributeValue = "";

        PathDataIteration pdi = row.getPathDataIteration();

        if (pdi != null) {
          List<InstanceAttribute> attributes = pdi.getInstanceAttributes();

          if (attributes != null) {
            jg.writeStartArray(attributeSelect);

            for (InstanceAttribute attribute : attributes) {
              InstanceAttributeDescriptor attributeDescriptor =
                  new InstanceAttributeDescriptor(attribute);

              if (attributeDescriptor.getName().equals(attributeSelectName)
                  && attributeDescriptor.getStringType().equals(attributeSelectType)) {

                attributeValue = attribute.getValue() + "";

                if (attribute instanceof InstanceDateAttribute) {
                  attributeValue =
                      getFormattedDate(((InstanceDateAttribute) attribute).getDateValue());
                } else if (attribute instanceof InstanceListOfValuesAttribute) {
                  attributeValue = ((InstanceListOfValuesAttribute) attribute).getSelectedName();
                }

                jg.write(attributeValue);
              }
            }

            jg.writeEnd();

          } else {
            jg.write(attributeSelect, attributeValue);
          }
        }
      }

      if (selects.contains(QueryField.CTX_PRODUCT_ID)) {
        String configurationItemId =
            queryContext != null ? queryContext.getConfigurationItemId() : "";
        jg.write(QueryField.CTX_PRODUCT_ID, configurationItemId);
      }
      if (selects.contains(QueryField.CTX_SERIAL_NUMBER)) {
        String serialNumber = queryContext != null ? queryContext.getSerialNumber() : "";
        jg.write(QueryField.CTX_SERIAL_NUMBER, serialNumber != null ? serialNumber : "");
      }
      if (selects.contains(QueryField.CTX_AMOUNT)) {
        String amount = row.getAmount() + "";
        jg.write(QueryField.CTX_AMOUNT, amount);
      }

      if (selects.contains(QueryField.CTX_P2P_SOURCE)) {
        Map<String, List<PartLinkList>> sources = row.getSources();
        String partLinksAsString = Tools.getPartLinksAsHumanString(sources);
        jg.write(QueryField.CTX_P2P_SOURCE, partLinksAsString);
      }

      if (selects.contains(QueryField.CTX_P2P_TARGET)) {
        Map<String, List<PartLinkList>> targets = row.getTargets();
        String partLinksAsString = Tools.getPartLinksAsHumanString(targets);
        jg.write(QueryField.CTX_P2P_TARGET, partLinksAsString);
      }

      if (selects.contains(QueryField.PART_MASTER_IS_STANDARD)) {
        boolean isStandard = row.getPartRevision().getPartMaster().isStandardPart();
        jg.write(QueryField.PART_MASTER_IS_STANDARD, isStandard);
      }

      jg.writeEnd();
    }

    jg.writeEnd();
    jg.flush();
  }