Exemplo n.º 1
0
 @Override
 protected AbstractCommonList getCommonList(UriInfo ui) {
   try {
     ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
     MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
     DocumentHandler handler = createDocumentHandler(ctx);
     String docType = queryParams.getFirst(IQueryManager.SEARCH_TYPE_DOCTYPE);
     String mode = queryParams.getFirst(IQueryManager.SEARCH_TYPE_INVOCATION_MODE);
     String whereClause = null;
     DocumentFilter documentFilter = null;
     String common_part = ctx.getCommonPartLabel();
     if (docType != null && !docType.isEmpty()) {
       whereClause = QueryManager.createWhereClauseForInvocableByDocType(common_part, docType);
       documentFilter = handler.getDocumentFilter();
       documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
     }
     if (mode != null && !mode.isEmpty()) {
       whereClause = QueryManager.createWhereClauseForInvocableByMode(common_part, mode);
       documentFilter = handler.getDocumentFilter();
       documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
     }
     if (whereClause != null && logger.isDebugEnabled()) {
       logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());
     }
     getRepositoryClient(ctx).getFiltered(ctx, handler);
     AbstractCommonList list = (AbstractCommonList) handler.getCommonPartList();
     return list;
   } catch (Exception e) {
     throw bigReThrow(e, ServiceMessages.LIST_FAILED);
   }
 }
  /**
   * Gets the relation list item, looking up the subject and object documents, and getting summary
   * info via the objectName and objectNumber properties in tenant-bindings.
   *
   * @param ctx the ctx
   * @param sbt the ServiceBindingType of Relations service
   * @param tReader the tenant-bindings reader, for looking up docnumber and docname
   * @param docModel the doc model
   * @param serviceContextPath the service context path
   * @return the relation list item, with nested subject and object summary info.
   * @throws Exception the exception
   */
  private RelationListItem getRelationListItem(
      ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
      ServiceBindingType sbt,
      TenantBindingConfigReaderImpl tReader,
      DocumentModel docModel,
      String serviceContextPath)
      throws Exception {
    RelationListItem relationListItem = new RelationListItem();
    String id = getCsid(docModel);
    relationListItem.setCsid(id);

    relationListItem.setSubjectCsid(
        (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_1));

    String predicate =
        (String)
            docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.RELATIONSHIP_TYPE);
    relationListItem.setRelationshipType(predicate);
    relationListItem.setPredicate(predicate); // predicate is new name for relationshipType.
    relationListItem.setPredicateDisplayName(
        (String)
            docModel.getProperty(
                ctx.getCommonPartLabel(), RelationJAXBSchema.RELATIONSHIP_TYPE_DISPLAYNAME));

    relationListItem.setObjectCsid(
        (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_ID_2));

    relationListItem.setUri(serviceContextPath + id);

    // Now fill in summary info for the related docs: subject and object.
    String subjectCsid = relationListItem.getSubjectCsid();
    String documentType =
        (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_TYPE_1);
    RelationsDocListItem subject =
        createRelationsDocListItem(ctx, sbt, subjectCsid, tReader, documentType);

    // Object o1 =  docModel.getProperty(ctx.getCommonPartLabel(), "subject");
    // Object o2 =  docModel.getProperty(ctx.getCommonPartLabel(), "object");

    String subjectUri =
        (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.subjectUri);
    subject.setUri(subjectUri);
    relationListItem.setSubject(subject);

    String objectCsid = relationListItem.getObjectCsid();
    String documentType2 =
        (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.DOCUMENT_TYPE_2);
    RelationsDocListItem object =
        createRelationsDocListItem(ctx, sbt, objectCsid, tReader, documentType2);

    String objectUri =
        (String) docModel.getProperty(ctx.getCommonPartLabel(), RelationJAXBSchema.objectUri);
    object.setUri(objectUri);
    relationListItem.setObject(object);

    return relationListItem;
  }
  /*
   * JAX-RS Annotated methods
   */
  @GET
  @Path("{csid}" + WorkflowClient.SERVICE_PATH)
  public byte[] getWorkflow(@PathParam("csid") String csid) {
    PoxPayloadOut result = null;

    try {
      ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();
      String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();

      MultipartServiceContext ctx =
          (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);
      WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
      ctx.setRespositoryWorkspaceName(
          parentWorkspaceName); // find the document in the parent's workspace
      getRepositoryClient(ctx).get(ctx, csid, handler);
      result = ctx.getOutput();
    } catch (Exception e) {
      throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
    }

    return result.getBytes();
  }
  protected Object getCommonPart() {
    Object result = null;

    try {
      MultipartServiceContext multiPartCtx = (MultipartServiceContext) getServiceContext();
      result = multiPartCtx.getInputPart(ctx.getCommonPartLabel(), getCommonPartClass());
    } catch (Exception e) {
      if (logger.isDebugEnabled() == true) {
        logger.debug("Could not extract common part from multipart input.", e);
      }
    }

    return result;
  }
  @Override
  public RelationsCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc)
      throws Exception {
    RelationsCommonList relList = this.extractPagingInfo(new RelationsCommonList(), wrapDoc);
    relList.setFieldsReturned(
        "subjectCsid|relationshipType|predicateDisplayName|objectCsid|uri|csid|subject|object");
    ServiceContext ctx = getServiceContext();
    String serviceContextPath = getServiceContextPath();

    TenantBindingConfigReaderImpl tReader =
        ServiceMain.getInstance().getTenantBindingConfigReader();
    String serviceName = getServiceContext().getServiceName().toLowerCase();
    ServiceBindingType sbt = tReader.getServiceBinding(ctx.getTenantId(), serviceName);

    Iterator<DocumentModel> iter = wrapDoc.getWrappedObject().iterator();
    while (iter.hasNext()) {
      DocumentModel docModel = iter.next();
      RelationListItem relListItem =
          getRelationListItem(ctx, sbt, tReader, docModel, serviceContextPath);
      relList.getRelationListItem().add(relListItem);
    }
    return relList;
  }
 @Override
 public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
     throws Exception {
   return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass());
 }
 /**
  * Creates the document handler.
  *
  * @param ctx the ctx
  * @param commonClass the common class
  * @return the document handler
  * @throws Exception the exception
  */
 public DocumentHandler createDocumentHandler(
     ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, Class<Object> commonClass) throws Exception {
   return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass);
 }
Exemplo n.º 8
0
  /**
   * Gets the element data.
   *
   * @param element the element
   * @param type the type
   * @return the element data
   */
  @SuppressWarnings("unchecked")
  private static Object getElementData(org.dom4j.Element element, Type type, ServiceContext ctx)
      throws Exception {
    Object result = null;
    String dateStr = "";

    if (type.isSimpleType()) {
      if (isNuxeoDateType(type)) {
        String dateVal = element.getText();
        if (dateVal == null || dateVal.trim().isEmpty()) {
          result = type.decode("");
        } else {
          // Dates or date/times in any ISO 8601-based representations
          // directly supported by Nuxeo will be successfully decoded.
          result = type.decode(dateVal);
          // All other date or date/time values must first be converted
          // to a supported ISO 8601-based representation.
          if (result == null) {
            dateStr = DateTimeFormatUtils.toIso8601Timestamp(dateVal, ctx.getTenantId());
            if (dateStr != null) {
              result = type.decode(dateStr);
            } else {
              throw new IllegalArgumentException(
                  "Unrecognized date value '" + dateVal + "' in field '" + element.getName() + "'");
            }
          }
        }
      } else {
        String textValue = element.getText();
        if (textValue != null && textValue.trim().isEmpty()) {
          result = null;
        } else {
          result = type.decode(textValue);
        }
      }
    } else if (type.isListType()) {
      ListType ltype = (ListType) type;
      List<Object> list = new ArrayList<Object>();
      Iterator<org.dom4j.Element> it = element.elementIterator();
      while (it.hasNext()) {
        org.dom4j.Element el = it.next();
        list.add(getElementData(el, ltype.getFieldType(), ctx));
      }
      Type ftype = ltype.getFieldType();
      if (ftype.isSimpleType()) { // these are stored as arrays
        Class klass = JavaTypes.getClass(ftype);
        if (klass.isPrimitive()) {
          return PrimitiveArrays.toPrimitiveArray(list, klass);
        } else {
          return list.toArray((Object[]) Array.newInstance(klass, list.size()));
        }
      }
      result = list;
    } else {
      ComplexType ctype = (ComplexType) type;
      if (ctype.getName().equals(TypeConstants.CONTENT)) {
        //				String mimeType = element.elementText(ExportConstants.BLOB_MIME_TYPE);
        //				String encoding = element.elementText(ExportConstants.BLOB_ENCODING);
        //				String content = element.elementTextTrim(ExportConstants.BLOB_DATA);
        //				if ((content == null || content.length() == 0)
        //						&& (mimeType == null || mimeType.length() == 0)) {
        //					return null; // remove blob
        //				}
        //				Blob blob = null;
        //				if (xdoc.hasExternalBlobs()) {
        //					blob = xdoc.getBlob(content);
        //				}
        //				if (blob == null) { // may be the blob is embedded like a Base64
        //					// encoded data
        //					byte[] bytes = Base64.decode(content);
        //					blob = new StreamingBlob(new ByteArraySource(bytes));
        //				}
        //				blob.setMimeType(mimeType);
        //				blob.setEncoding(encoding);
        //				return blob;
      } else { // a complex type
        Map<String, Object> map = new HashMap<String, Object>();
        Iterator<org.dom4j.Element> it = element.elementIterator();
        while (it.hasNext()) {
          org.dom4j.Element el = it.next();
          String name = el.getName();
          Object value = getElementData(el, ctype.getField(el.getName()).getType(), ctx);
          map.put(name, value);
        }
        result = map;
      }
    }
    return result;
  }
  protected RelationsDocListItem createRelationsDocListItem(
      ServiceContext ctx,
      ServiceBindingType sbt,
      String itemCsid,
      TenantBindingConfigReaderImpl tReader,
      String documentType)
      throws Exception {
    RelationsDocListItem item = new RelationsDocListItem();
    item.setDocumentType(
        documentType); // this one comes from the record, as documentType1, documentType2.
    // CSPACE-4037 REMOVING: item.setService(documentType);//this one comes from the record, as
    // documentType1, documentType2.   Current app seems to use servicename for this.
    item.setCsid(itemCsid);

    DocumentModel itemDocModel =
        NuxeoUtils.getDocFromCsid(getRepositorySession(), ctx, itemCsid); // null if not found.
    if (itemDocModel != null) {
      String itemDocType = itemDocModel.getDocumentType().getName();
      // CSPACE-4037 REMOVING: item.setDocumentTypeFromModel(itemDocType);           //this one
      // comes from the nuxeo documentType

      // DEBUG: System.out.println("\r\n******** AuthorityItemDocumentModelHandlder documentType
      // **************\r\n\tdocModel: "+itemDocType+"\r\n\tpayload: "+documentType);
      // boolean usedDocumentTypeFromPayload = true;
      /*if ( ! Tools.isBlank(documentType)){
          if (documentType.equals(itemDocType)){
              //usedDocumentTypeFromPayload = true;
          }  else {
              // Laramie20110510 CSPACE-3739  throw the exception for 3739, otherwise, don't throw it.
              //throw new Exception("documentType supplied was wrong.  supplied: "+documentType+" required: "+itemDocType+ " itemCsid: "+itemCsid );
          }
      } else {
          //usedDocumentTypeFromPayload = false;
          item.setDocumentType(itemDocType);
      }   */
      if (Tools.isBlank(documentType)) {
        item.setDocumentType(itemDocType);
      }

      // TODO: clean all the output statements out of here when CSPACE-4037 is done.
      // TODO: ensure that itemDocType is really the entry point, i.e. servicename==doctype
      // ServiceBindingType itemSbt2 = tReader.getServiceBinding(ctx.getTenantId(), itemDocType);
      String propName = "ERROR-FINDING-PROP-VALUE";
      ServiceBindingType itemSbt =
          tReader.getServiceBindingForDocType(ctx.getTenantId(), itemDocType);
      try {
        propName = ServiceBindingUtils.getPropertyValue(itemSbt, ServiceBindingUtils.OBJ_NAME_PROP);
        String itemDocname =
            ServiceBindingUtils.getMappedFieldInDoc(
                itemSbt, ServiceBindingUtils.OBJ_NAME_PROP, itemDocModel);
        if (propName == null || itemDocname == null) {
          // System.out.println("=== prop NOT found:
          // "+ServiceBindingUtils.OBJ_NAME_PROP+"::"+propName+"="+itemDocname+" documentType:
          // "+documentType);
        } else {
          item.setName(itemDocname);
          // System.out.println("=== found prop :
          // "+ServiceBindingUtils.OBJ_NAME_PROP+"::"+propName+"="+itemDocname+" documentType:
          // "+documentType);
        }
      } catch (Throwable t) {
        System.out.println(
            "====Error finding objectNameProperty: "
                + itemDocModel
                + " field "
                + ServiceBindingUtils.OBJ_NAME_PROP
                + "="
                + propName
                + " not found in itemDocType: "
                + itemDocType
                + " inner: "
                + t.getMessage());
      }
      propName = "ERROR-FINDING-PROP-VALUE";
      try {
        propName =
            ServiceBindingUtils.getPropertyValue(itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP);
        String itemDocnumber =
            ServiceBindingUtils.getMappedFieldInDoc(
                itemSbt, ServiceBindingUtils.OBJ_NUMBER_PROP, itemDocModel);

        if (propName == null || itemDocnumber == null) {
          // System.out.println("=== prop NOT found:
          // "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
          //                          +" documentType: "+documentType);
        } else {
          item.setNumber(itemDocnumber);
          // System.out.println("============ found prop :
          // "+ServiceBindingUtils.OBJ_NUMBER_PROP+"::"+propName+"="+itemDocnumber
          //                          +" documentType: "+documentType);
        }
      } catch (Throwable t) {
        logger.error(
            "====Error finding objectNumberProperty: "
                + ServiceBindingUtils.OBJ_NUMBER_PROP
                + "="
                + propName
                + " not found in itemDocType: "
                + itemDocType
                + " inner: "
                + t.getMessage());
      }
    } else {
      item.setError("INVALID: related object is absent");
      // Laramie20110510 CSPACE-3739  throw the exception for 3739, otherwise, don't throw it.
      // throw new Exception("INVALID: related object is absent "+itemCsid);
    }
    return item;
  }