You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/08/04 02:11:02 UTC

[GitHub] [inlong] healchow commented on a diff in pull request #5343: [INLONG-4750][Manager] Merge the workflow util classes into one class

healchow commented on code in PR #5343:
URL: https://github.com/apache/inlong/pull/5343#discussion_r937285629


##########
inlong-manager/manager-workflow/src/main/java/org/apache/inlong/manager/workflow/util/WorkflowUtils.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.inlong.manager.workflow.util;
+
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.Lists;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.manager.common.enums.ProcessStatus;
+import org.apache.inlong.manager.common.enums.TaskStatus;
+import org.apache.inlong.manager.common.exceptions.FormParseException;
+import org.apache.inlong.manager.common.exceptions.JsonException;
+import org.apache.inlong.manager.common.util.Preconditions;
+import org.apache.inlong.manager.dao.entity.WorkflowEventLogEntity;
+import org.apache.inlong.manager.dao.entity.WorkflowProcessEntity;
+import org.apache.inlong.manager.dao.entity.WorkflowTaskEntity;
+import org.apache.inlong.manager.pojo.workflow.ListenerExecuteLog;
+import org.apache.inlong.manager.pojo.workflow.ProcessResponse;
+import org.apache.inlong.manager.pojo.workflow.TaskExecuteLog;
+import org.apache.inlong.manager.pojo.workflow.TaskResponse;
+import org.apache.inlong.manager.pojo.workflow.WorkflowResult;
+import org.apache.inlong.manager.pojo.workflow.form.process.ProcessForm;
+import org.apache.inlong.manager.pojo.workflow.form.task.TaskForm;
+import org.apache.inlong.manager.workflow.WorkflowContext;
+import org.apache.inlong.manager.workflow.WorkflowContext.ActionContext;
+import org.apache.inlong.manager.workflow.definition.UserTask;
+import org.apache.inlong.manager.workflow.definition.WorkflowProcess;
+import org.apache.inlong.manager.workflow.definition.WorkflowTask;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+
+/**
+ * Workflow utils
+ */
+public class WorkflowUtils {
+
+    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+    private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowUtils.class);
+
+    /**
+     * Build workflow context from WorkflowProcess and WorkflowProcessEntity
+     */
+    public static WorkflowContext buildContext(ObjectMapper objectMapper, WorkflowProcess process,
+            WorkflowProcessEntity processEntity) {
+        try {
+            ProcessForm form = WorkflowUtils.parseProcessForm(objectMapper, processEntity.getFormData(), process);
+            return new WorkflowContext().setProcess(process)
+                    .setOperator(processEntity.getApplicant())
+                    .setProcessForm(form)
+                    .setProcessEntity(processEntity);
+        } catch (Exception e) {
+            LOGGER.error("build context from process form failed with id=" + processEntity.getId(), e);
+            return null;
+        }
+    }
+
+    /**
+     * Get the workflow result from the given workflow context
+     */
+    public static WorkflowResult getResult(WorkflowContext context) {
+        if (context == null) {
+            return null;
+        }
+
+        WorkflowResult workflowResult = new WorkflowResult();
+        workflowResult.setProcessInfo(WorkflowUtils.getProcessResponse(context.getProcessEntity()));
+        if (context.getActionContext() != null) {
+            ActionContext newAction = context.getActionContext();
+            workflowResult.setNewTasks(Lists.newArrayList(WorkflowUtils.getTaskResponse(newAction.getTaskEntity())));
+        }
+        return workflowResult;
+    }
+
+    /**
+     * Get process response from process entity
+     */
+    public static ProcessResponse getProcessResponse(WorkflowProcessEntity entity) {
+        if (entity == null) {
+            return null;
+        }
+
+        ProcessResponse processResponse = ProcessResponse.builder()
+                .id(entity.getId())
+                .name(entity.getName())
+                .displayName(entity.getDisplayName())
+                .type(entity.getType())
+                .title(entity.getTitle())
+                .applicant(entity.getApplicant())
+                .status(ProcessStatus.valueOf(entity.getStatus()))
+                .startTime(entity.getStartTime())
+                .endTime(entity.getEndTime())
+                .build();
+        try {
+            if (StringUtils.isNotBlank(entity.getFormData())) {
+                processResponse.setFormData(OBJECT_MAPPER.readTree(entity.getFormData()));
+            }
+            if (StringUtils.isNotBlank(entity.getExtParams())) {
+                processResponse.setExtParams(OBJECT_MAPPER.readTree(entity.getExtParams()));
+            }
+        } catch (Exception e) {
+            LOGGER.error("parse process form error: ", e);

Review Comment:
   The exception stack trace will print all info, such as:
   
   <img width="1241" alt="image" src="https://user-images.githubusercontent.com/31994335/182747891-8010fd0d-71db-4360-a10d-b98f834c680a.png">
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org