@Override
  public void handleEvent(Event event) throws ClientException {

    if (!DOCUMENT_CREATED.equals(event.getName())) {
      return;
    }
    EventContext ctx = event.getContext();
    if (ctx instanceof DocumentEventContext) {
      DocumentEventContext docCtx = (DocumentEventContext) ctx;
      DocumentModel doc = docCtx.getSourceDocument();
      if (doc.isProxy() || doc.isVersion()) {
        // a proxy or version keeps the uid of the document
        // being proxied or versioned => we're not allowed
        // to modify its field.
        return;
      }
      String eventId = event.getName();
      log.debug("eventId : " + eventId);
      try {
        addUIDtoDoc(doc);
      } catch (DocumentException e) {
        log.error("Error occurred while generating UID for doc: " + doc, e);
      }
    }
  }
Esempio n. 2
0
  @Override
  public void handleEvent(Event event) {
    EventContext eventContext = event.getContext();
    Serializable property =
        eventContext.getProperty(TaskService.TASK_INSTANCE_EVENT_PROPERTIES_KEY);
    if (property == null || !(property instanceof Task)) {
      // do nothing
      return;
    }
    Task task = (Task) property;

    Boolean validated =
        Boolean.valueOf(task.getVariable(TaskService.VariableName.validated.name()));

    String chain;
    if (validated) {
      chain = task.getVariable(OperationTaskVariableName.acceptOperationChain.name());
    } else {
      chain = task.getVariable(OperationTaskVariableName.rejectOperationChain.name());
    }

    if (!StringUtils.isEmpty(chain)) {
      try {
        // run the given operation
        AutomationService os = Framework.getService(AutomationService.class);
        OperationContext ctx = new OperationContext(eventContext.getCoreSession());
        if (eventContext instanceof DocumentEventContext) {
          ctx.setInput(((DocumentEventContext) eventContext).getSourceDocument());
          ctx.put(OperationTaskVariableName.taskDocument.name(), task.getDocument());
        }
        try {
          os.run(ctx, chain);
        } catch (InvalidChainException e) {
          log.error("Unknown chain: " + chain);
        }
      } catch (OperationException t) {
        log.error(t, t);
      }
    }
  }
  @Override
  public void handleEvent(Event event) throws ClientException {

    logger.trace("In handleEvent in UpdateObjectLocationOnMove ...");

    EventContext eventContext = event.getContext();
    if (eventContext == null) {
      return;
    }

    if (!(eventContext instanceof DocumentEventContext)) {
      return;
    }
    DocumentEventContext docEventContext = (DocumentEventContext) eventContext;
    DocumentModel docModel = docEventContext.getSourceDocument();

    // If this document event involves a Relation record, does this pertain to
    // a relationship between a Movement record and a CollectionObject record?
    //
    // If not, we're not interested in processing this document event
    // in this event handler, as it will have no bearing on updating a
    // computed current location for a CollectionObject.

    //
    // (The rest of the code flow below is then identical to that which
    // is followed when this document event involves a Movement record.
    String movementCsid = "";
    Enum notificationDocumentType;
    if (documentMatchesType(docModel, RELATION_DOCTYPE)) {
      if (logger.isTraceEnabled()) {
        logger.trace(
            "An event involving a Relation document was received by UpdateObjectLocationOnMove ...");
      }
      // Get a Movement CSID from the Relation record. (If we can't
      // get it, then we don't have a pertinent relation record.)
      movementCsid =
          getCsidForDesiredDocTypeFromRelation(
              docModel, MOVEMENT_DOCTYPE, COLLECTIONOBJECT_DOCTYPE);
      if (Tools.isBlank(movementCsid)) {
        logger.warn("Could not obtain CSID for Movement record from document event.");
        logger.warn(NO_FURTHER_PROCESSING_MESSAGE);
        return;
      }
      notificationDocumentType = EventNotificationDocumentType.RELATION;
    } else if (documentMatchesType(docModel, MOVEMENT_DOCTYPE)) {
      // Otherwise, get a Movement CSID directly from the Movement record.
      if (logger.isTraceEnabled()) {
        logger.trace(
            "An event involving a Movement document was received by UpdateObjectLocationOnMove ...");
      }
      // FIXME: exclude creation events for Movement records here, if we can
      // identify that we'l still be properly handling creation events
      // that include a relations list as part of the creation payload,
      // perhaps because that may trigger a separate event notification.
      movementCsid = NuxeoUtils.getCsid(docModel);
      if (Tools.isBlank(movementCsid)) {
        logger.warn("Could not obtain CSID for Movement record from document event.");
        logger.warn(NO_FURTHER_PROCESSING_MESSAGE);
        return;
      }
      notificationDocumentType = EventNotificationDocumentType.MOVEMENT;
    } else {
      if (logger.isTraceEnabled()) {
        logger.trace(
            "This event did not involve a document relevant to UpdateObjectLocationOnMove ...");
      }
      return;
    }

    // Note: currently, all Document lifecycle transitions on
    // the relevant doctype(s) are handled by this event handler,
    // not just transitions between 'soft deleted' and active states.
    //
    // We are assuming that we'll want to re-compute current locations
    // for related CollectionObjects on all such transitions, as the
    // semantics of such transitions are opaque to this event handler,
    // because arbitrary workflows can be bound to those doctype(s).
    //
    // If we need to filter out some of those lifecycle transitions,
    // such as excluding transitions to the 'locked' workflow state; or,
    // alternately, if we want to restrict this event handler's
    // scope to handle only transitions into the 'soft deleted' state,
    // we can add additional checks for doing so at this point in the code.

    // For debugging
    if (logger.isTraceEnabled()) {
      logger.trace("Movement CSID=" + movementCsid);
      logger.trace("Notification document type=" + notificationDocumentType.name());
    }

    CoreSession coreSession = docEventContext.getCoreSession();
    Set<String> collectionObjectCsids = new HashSet<>();

    if (notificationDocumentType == EventNotificationDocumentType.RELATION) {
      String relatedCollectionObjectCsid =
          getCsidForDesiredDocTypeFromRelation(
              docModel, COLLECTIONOBJECT_DOCTYPE, MOVEMENT_DOCTYPE);
      collectionObjectCsids.add(relatedCollectionObjectCsid);
    } else if (notificationDocumentType == EventNotificationDocumentType.MOVEMENT) {
      collectionObjectCsids.addAll(
          getCollectionObjectCsidsRelatedToMovement(movementCsid, coreSession));
    }

    if (collectionObjectCsids.isEmpty()) {
      logger.warn("Could not obtain any CSIDs of related CollectionObject records.");
      logger.warn(NO_FURTHER_PROCESSING_MESSAGE);
      return;
    } else {
      if (logger.isTraceEnabled()) {
        logger.trace(
            "Found "
                + collectionObjectCsids.size()
                + " CSIDs of related CollectionObject records.");
      }
    }
    // Iterate through the list of CollectionObject CSIDs found.
    // For each CollectionObject, obtain its most recent, related Movement,
    // and update relevant field(s) with values from that Movement record.
    DocumentModel collectionObjectDocModel;
    DocumentModel mostRecentMovementDocModel;
    for (String collectionObjectCsid : collectionObjectCsids) {
      if (logger.isTraceEnabled()) {
        logger.trace("CollectionObject CSID=" + collectionObjectCsid);
      }
      // Verify that the CollectionObject is retrievable.
      collectionObjectDocModel = getDocModelFromCsid(coreSession, collectionObjectCsid);
      if (collectionObjectDocModel == null) {
        continue;
      }
      // Verify that the CollectionObject record is active.
      if (!isActiveDocument(collectionObjectDocModel)) {
        continue;
      }
      // Get the CollectionObject's most recent, related Movement.
      mostRecentMovementDocModel = getMostRecentMovement(coreSession, collectionObjectCsid);
      if (mostRecentMovementDocModel == null) {
        continue;
      }
      // Update the CollectionObject with values from that Movement.
      collectionObjectDocModel =
          updateCollectionObjectValuesFromMovement(
              collectionObjectDocModel, mostRecentMovementDocModel);
      coreSession.saveDocument(collectionObjectDocModel);
    }
  }