All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xworker.app.userflow.UserTaskCondition Maven / Gradle / Ivy

The newest version!
package xworker.app.userflow;

import org.xmeta.ActionContext;
import org.xmeta.Thing;
import org.xmeta.util.UtilDate;
import xworker.dataObject.DataObject;
import xworker.lang.executor.Executor;
import xworker.lang.task.UserTask;
import xworker.lang.task.cron.CronExpression;
import xworker.lang.util.UtilData;

import java.util.Date;
import java.util.List;

public class UserTaskCondition {
    private static final String TAG = UserTaskCondition.class.getName();

    //xworker.app.userflow.UserTaskCondition/@actions/@isTrue
    public static boolean checkUserTaskCondition(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        for(Thing child : self.getChilds()){
            if(!UtilData.isTrue(child.doAction("isTure", actionContext))){
                return false;
            }
        }

        return true;
    }

    public static boolean checkOr(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        for(Thing child : self.getChilds()){
            if(UtilData.isTrue(child.doAction("isTure", actionContext))){
                return true;
            }
        }

        return false;
    }

    //xworker.app.userflow.UserTaskCondition/@TasksCompleted/@actions/@isTrue
    public static boolean checkTasksCompleted(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        List tasks = self.doAction("getTaskThings", actionContext);
        if(tasks != null){
            int finishedCount = self.getInt("finishedCount");
            boolean ignoreCurrentStatus = self.getBoolean("ignoreCurrentStatus");

            for(Thing taskThing : tasks){
                DataObject task = UserFlowActions.getTask(taskThing, actionContext);
                if(task == null){
                    return false;
                }

                if(task.getInt("finishedCount") < finishedCount){
                    return false;
                }

                if(!ignoreCurrentStatus && task.getInt("status") <= 1) {
                    //任务当前状态不是已完成状态
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    //xworker.app.userflow.UserTaskCondition/@TasksNotCompleted/@actions/@isTrue
    public static boolean checkTasksNotCompleted(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        List tasks = self.doAction("getTaskThings", actionContext);
        if(tasks != null){
            int finishedCount = self.getInt("finishedCount");
            boolean checkCurrentStatus = self.getBoolean("checkCurrentStatus");

            for(Thing taskThing : tasks){
                DataObject task = UserFlowActions.getTask(taskThing, actionContext);
                if(task == null){
                    continue;
                }

                if(checkCurrentStatus && task.getInt("status") > 1){
                    return false;
                }

                if(task.getInt("finishedCount") > finishedCount){
                    return false;
                }
            }

        }
        return true;
    }

    //xworker.app.userflow.UserTaskCondition/@Daily/@actions/@isTrue
    public static boolean checkDaily(ActionContext actionContext){
        DataObject task = actionContext.getObject("task");

        return task.getInt("status") == UserTask.FINISHED && task.getDate("lastFinishedTime") != null
                && !UtilDate.isToday(task.getDate("lastFinishedTime"));
    }

    //xworker.app.userflow.UserTaskCondition/@CronCondition/@actions/@isTrue
    public static boolean checkCronCondition(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        String cron = self.getStringBlankAsNull("cron");
        if(cron != null){
            try {
                CronExpression expression = new CronExpression(cron);

                return expression.isSatisfiedBy(new Date());
            }catch(Exception e){
                Executor.warn(TAG, "Check cron condition error, path=" + self.getMetadata().getPath(), e);
            }
        }

        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy