Search in sources :

Example 1 with SubstitutesDataModel

use of org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel in project carbon-business-process by wso2.

the class UserSubstitutionService method querySubstitutes.

/**
 * Query the substitution records based on substitute, assignee and enabled or disabled.
 * Pagination parameters, start, size, sort, order are allowed.
 * @return paginated list of substitution info records
 */
@GET
@Path("/")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response querySubstitutes() {
    if (!subsFeatureEnabled) {
        return Response.status(405).build();
    }
    Map<String, String> queryMap = new HashedMap();
    for (Map.Entry<String, String> entry : propertiesMap.entrySet()) {
        String value = uriInfo.getQueryParameters().getFirst(entry.getKey());
        if (value != null) {
            queryMap.put(entry.getValue(), value);
        }
    }
    // validate the parameters
    try {
        // replace with tenant aware user names
        String tenantAwareUser = getTenantAwareUser(queryMap.get(SubstitutionQueryProperties.USER));
        queryMap.put(SubstitutionQueryProperties.USER, tenantAwareUser);
        String tenantAwareSub = getTenantAwareUser(queryMap.get(SubstitutionQueryProperties.SUBSTITUTE));
        queryMap.put(SubstitutionQueryProperties.SUBSTITUTE, tenantAwareSub);
        if (!isUserAuthorizedForSubstitute(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername())) {
            String loggedInUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
            if (!((queryMap.get(SubstitutionQueryProperties.USER) != null && queryMap.get(SubstitutionQueryProperties.USER).equals(loggedInUser)) || (queryMap.get(SubstitutionQueryProperties.SUBSTITUTE) != null && queryMap.get(SubstitutionQueryProperties.SUBSTITUTE).equals(loggedInUser)))) {
                throw new BPMNForbiddenException("Not allowed to view others substitution details. No sufficient permission");
            }
        }
    } catch (UserStoreException e) {
        throw new ActivitiException("Error accessing User Store for input validations", e);
    }
    // validate pagination parameters
    validatePaginationParams(queryMap);
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
    List<SubstitutesDataModel> dataModelList = UserSubstitutionUtils.querySubstitutions(queryMap, tenantId);
    int totalResultCount = UserSubstitutionUtils.getQueryResultCount(queryMap, tenantId);
    SubstituteInfoCollectionResponse collectionResponse = new SubstituteInfoCollectionResponse();
    collectionResponse.setTotal(totalResultCount);
    List<SubstituteInfoResponse> responseList = new ArrayList<>();
    for (SubstitutesDataModel subsData : dataModelList) {
        SubstituteInfoResponse response = new SubstituteInfoResponse();
        response.setEnabled(subsData.isEnabled());
        response.setEndTime(subsData.getSubstitutionEnd());
        response.setStartTime(subsData.getSubstitutionStart());
        response.setSubstitute(subsData.getSubstitute());
        response.setAssignee(subsData.getUser());
        responseList.add(response);
    }
    collectionResponse.setSubstituteInfoList(responseList);
    collectionResponse.setSize(responseList.size());
    String sortType = getSortType(queryMap.get(SubstitutionQueryProperties.SORT));
    collectionResponse.setSort(sortType);
    collectionResponse.setStart(Integer.parseInt(queryMap.get(SubstitutionQueryProperties.START)));
    collectionResponse.setOrder(queryMap.get(SubstitutionQueryProperties.ORDER));
    return Response.ok(collectionResponse).build();
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) SubstitutesDataModel(org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel) BPMNForbiddenException(org.wso2.carbon.bpmn.rest.common.exception.BPMNForbiddenException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) HashedMap(org.apache.commons.collections.map.HashedMap) HashedMap(org.apache.commons.collections.map.HashedMap)

Example 2 with SubstitutesDataModel

use of org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel in project carbon-business-process by wso2.

the class UserSubstitutionUtils method handleScheduledEventByTenant.

public static synchronized boolean handleScheduledEventByTenant(int tenantId) {
    boolean result = true;
    TransitivityResolver resolver = SubstitutionDataHolder.getInstance().getTransitivityResolver();
    ActivitiDAO activitiDAO = SubstitutionDataHolder.getInstance().getActivitiDAO();
    if (SubstitutionDataHolder.getInstance().isTransitivityEnabled()) {
        // update transitives, only the map is updated here
        result = resolver.resolveTransitiveSubs(true, tenantId);
    } else {
        resolver.subsMap = activitiDAO.selectActiveSubstitutesByTenant(tenantId, new Date(System.currentTimeMillis()));
    }
    // flush into db
    for (Map.Entry<String, SubstitutesDataModel> entry : resolver.subsMap.entrySet()) {
        // go through the updated map
        SubstitutesDataModel model = entry.getValue();
        try {
            // set carbon context
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext context = PrivilegedCarbonContext.getThreadLocalCarbonContext();
            context.setUsername(model.getUser());
            context.setTenantId(tenantId, true);
            if (SubstitutionDataHolder.getInstance().isTransitivityEnabled()) {
                activitiDAO.updateSubstituteInfo(model);
            }
            if (!BPMNConstants.BULK_REASSIGN_PROCESSED.equals(model.getTaskList())) {
                // active substitution, not yet bulk reassigned
                String sub = getActualSubstitute(model);
                if (model.getTaskList() == null) {
                    // reassign all
                    if (sub != null) {
                        bulkReassign(model.getUser(), sub, null);
                    } else {
                        // transitivity undefined, assign to task owner or un-claim
                        assignToTaskOwner(model.getUser(), null);
                    }
                } else {
                    List<String> taskList = getTaskListFromString(model.getTaskList());
                    if (sub != null) {
                        bulkReassign(model.getUser(), sub, taskList);
                    } else {
                        // transitivity undefined, assign to task owner or un-claim
                        assignToTaskOwner(model.getUser(), taskList);
                    }
                }
                model.setTaskList(BPMNConstants.BULK_REASSIGN_PROCESSED);
                activitiDAO.updateSubstituteInfo(model);
            }
        } finally {
            PrivilegedCarbonContext.endTenantFlow();
            PrivilegedCarbonContext.destroyCurrentContext();
        }
    }
    // disable expired records
    disableExpiredRecords(tenantId);
    return result;
}
Also used : PaginatedSubstitutesDataModel(org.wso2.carbon.bpmn.core.mgt.model.PaginatedSubstitutesDataModel) SubstitutesDataModel(org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel) ActivitiDAO(org.wso2.carbon.bpmn.core.mgt.dao.ActivitiDAO) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext)

Example 3 with SubstitutesDataModel

use of org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel in project carbon-business-process by wso2.

the class UserSubstitutionUtils method handleUpdateSubstitute.

public static void handleUpdateSubstitute(String assignee, String substitute, Date startTime, Date endTime, boolean enabled, List<String> taskList, int tenantId) {
    ActivitiDAO activitiDAO = SubstitutionDataHolder.getInstance().getActivitiDAO();
    SubstitutesDataModel existingSubInfo = activitiDAO.selectSubstituteInfo(assignee, tenantId);
    if (existingSubInfo != null) {
        // need to put existing values for null columns, if not existing data may replace by Null
        if (startTime == null) {
            startTime = existingSubInfo.getSubstitutionStart();
        }
        if (endTime == null) {
            endTime = existingSubInfo.getSubstitutionEnd();
        }
        String taskListString = getTaskListString(taskList);
        if (taskList == null) {
            taskListString = existingSubInfo.getTaskList();
        }
        SubstitutesDataModel dataModel = updateSubstituteInfo(assignee, substitute, startTime, endTime, taskListString, tenantId);
        if (dataModel.isEnabled() && isBeforeActivationInterval(dataModel.getSubstitutionStart())) {
            boolean transitivityResolved = updateTransitiveSubstitutes(dataModel, tenantId);
            if (!transitivityResolved) {
                // remove added transitive record
                activitiDAO.updateSubstituteInfo(existingSubInfo);
                throw new SubstitutionException("Could not find an available substitute. Use a different user to substitute");
            }
            if (SubstitutionDataHolder.getInstance().isTransitivityEnabled()) {
                // transitive substitute maybe changed, need to retrieve again.
                dataModel = activitiDAO.selectSubstituteInfo(dataModel.getUser(), dataModel.getTenantId());
            }
            if (!SubstitutionDataHolder.getInstance().isTransitivityEnabled() || BPMNConstants.TRANSITIVE_SUB_NOT_APPLICABLE.equals(dataModel.getTransitiveSub())) {
                bulkReassign(dataModel.getUser(), dataModel.getSubstitute(), taskList);
            } else {
                bulkReassign(dataModel.getUser(), dataModel.getTransitiveSub(), taskList);
            }
        }
    } else {
        throw new SubstitutionException("Substitute for user: " + assignee + ", does not exist. Try to add new substitute info record");
    }
}
Also used : PaginatedSubstitutesDataModel(org.wso2.carbon.bpmn.core.mgt.model.PaginatedSubstitutesDataModel) SubstitutesDataModel(org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel) ActivitiDAO(org.wso2.carbon.bpmn.core.mgt.dao.ActivitiDAO)

Example 4 with SubstitutesDataModel

use of org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel in project carbon-business-process by wso2.

the class UserSubstitutionUtils method getSubstituteOfUser.

/**
 * Get the substitute info of the given user.
 * @param assignee
 * @return SubstitutesDataModel
 */
public static SubstitutesDataModel getSubstituteOfUser(String assignee, int tenantId) {
    SubstitutesDataModel dataModel = SubstitutionDataHolder.getInstance().getActivitiDAO().selectSubstituteInfo(assignee, tenantId);
    Date maxDate = getEndTimeMaxDate();
    // set null if max end date
    if (dataModel != null && maxDate.compareTo(dataModel.getSubstitutionEnd()) == 0) {
        dataModel.setSubstitutionEnd(null);
    }
    return dataModel;
}
Also used : PaginatedSubstitutesDataModel(org.wso2.carbon.bpmn.core.mgt.model.PaginatedSubstitutesDataModel) SubstitutesDataModel(org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel)

Example 5 with SubstitutesDataModel

use of org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel in project carbon-business-process by wso2.

the class UserSubstitutionUtils method disableExpiredRecords.

/**
 * Disable the records that are still enabled but expired
 * @param tenantId
 */
public static void disableExpiredRecords(int tenantId) {
    ActivitiDAO activitiDAO = SubstitutionDataHolder.getInstance().getActivitiDAO();
    Map<String, SubstitutesDataModel> map = activitiDAO.getEnabledExpiredRecords(tenantId, new Date(System.currentTimeMillis()));
    for (Map.Entry<String, SubstitutesDataModel> entry : map.entrySet()) {
        activitiDAO.enableSubstitution(false, entry.getKey(), tenantId);
    }
}
Also used : PaginatedSubstitutesDataModel(org.wso2.carbon.bpmn.core.mgt.model.PaginatedSubstitutesDataModel) SubstitutesDataModel(org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel) ActivitiDAO(org.wso2.carbon.bpmn.core.mgt.dao.ActivitiDAO)

Aggregations

SubstitutesDataModel (org.wso2.carbon.bpmn.core.mgt.model.SubstitutesDataModel)15 PaginatedSubstitutesDataModel (org.wso2.carbon.bpmn.core.mgt.model.PaginatedSubstitutesDataModel)11 ActivitiDAO (org.wso2.carbon.bpmn.core.mgt.dao.ActivitiDAO)7 AbstractCustomSqlExecution (org.activiti.engine.impl.cmd.AbstractCustomSqlExecution)3 SubstitutesMapper (org.wso2.carbon.bpmn.core.internal.mapper.SubstitutesMapper)3 Date (java.util.Date)2 List (java.util.List)2 RowBounds (org.apache.ibatis.session.RowBounds)2 BPMNForbiddenException (org.wso2.carbon.bpmn.rest.common.exception.BPMNForbiddenException)2 ActivitiException (org.activiti.engine.ActivitiException)1 HashedMap (org.apache.commons.collections.map.HashedMap)1 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)1 UserStoreException (org.wso2.carbon.user.api.UserStoreException)1