
com.adobe.aem.formsndocuments.util.RnCUtil Maven / Gradle / Ivy
/***************************************************************************
* ADOBE CONFIDENTIAL
* ___________________
*
* Copyright 2013 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
package com.adobe.aem.formsndocuments.util;
import java.security.Principal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import com.adobe.aem.formsndocuments.bootstrap.PrincipalImpl;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.aem.formsndocuments.exception.FormsNDocumentsException;
import com.adobe.granite.taskmanagement.Task;
import com.adobe.granite.taskmanagement.TaskManager;
import com.adobe.granite.taskmanagement.TaskManagerException;
import com.adobe.granite.workflow.exec.Status;
public class RnCUtil {
public static final String PROJECT_TYPE = "project";
public static final String PROJECT_SUB_TYPE = "projectsubtask";
public static final String TASK_DUE_DATE = "taskDueDate";
public static final String ASSET_PATH = "assetPath";
public static final String REVIEWERS = "reviewers";
public static final String REVIEW_NAME = "reviewName";
public static final String REVIEW_ID = "reviewId";
public static final String REVIEW_INITIATOR = "reviewInitiator";
public static final String REVIEW_DESCRIPTION = "reviewDescription";
public static final String REVIEW_PROJECT_NAME = "reviewProjectName";
public static final String REVIEW_GROUP = "reviewGroup";
public static final String REVIEW_DEADLINE = "reviewDeadline";
public static final String UNDER_REVIEW = "underReview";
public static final String REVIEW_CONTAINER_NODE_NAME = "reviewcontainer";
public static final String REVIEW_TASKS_PATH = "/jcr:content/reviewcontainer/reviewtasks";
private static final String USER_ID = "User Id";
private static final String GROUP_ID = "Group Id";
private static final String CRX_PATH = "CRX Path";
private static final String PROJECT_NAME = "Project Name";
// DateFormat "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" 2013-12-28T23:59:00.000+05:30
// to 2014-01-01T20:30:00.538Z
private static SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS");
private static Logger logger = LoggerFactory.getLogger(RnCUtil.class);
/**
* Decides whether the task status passed, makes the task eligible for
* update or not.
*
* @param status
* task status
* @return boolean
*/
public static boolean isTaskEligibleForUpdate(Status status) {
if (Status.COMPLETE.equals(status) || Status.TERMINATED.equals(status)) {
return false;
}
return true;
}
/**
* Checks whether the user is member of group or not.
*
* @param groupId
* @param userId
* @param resourceResolver
* @return
* @throws FormsNDocumentsException
* @throws RepositoryException
*/
public static Boolean isMemberOfGroup(String groupId, String userId,
ResourceResolver resourceResolver) throws FormsNDocumentsException,
RepositoryException {
logger.trace("Entering isMemberOfGroup.");
if (groupId == null || (groupId != null && groupId.isEmpty())) {
Object[] args = { GROUP_ID, groupId };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
if (userId == null || (userId != null && userId.isEmpty())) {
Object[] args = { USER_ID, userId };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
UserManager um = resourceResolver.adaptTo(UserManager.class);
User userAuthorizable = (User)um.getAuthorizable(userId);
if (userAuthorizable == null) {
Object[] args = { USER_ID, userId };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
//returns true if the user is 'admin'
if(userAuthorizable.isAdmin())
{
return true;
}
//returns true if the user is member of the group that is being passed as input parameter.
Iterator grpItr = userAuthorizable.memberOf();
while (grpItr.hasNext()) {
Group group = grpItr.next();
if (groupId.equals(group.getID())) {
return true;
}
}
logger.trace("Exiting isMemberOfGroup.");
return false;
}
/**
* Returns the members of the specified group.
*
* @param groupId
* @param resourceResolver
* @return
* @throws FormsNDocumentsException
* @throws RepositoryException
*/
public static List getGroupMembers(String groupId,
ResourceResolver resourceResolver) throws FormsNDocumentsException,
RepositoryException {
logger.trace("Entering getGroupMembers.");
if (groupId == null || (groupId != null && groupId.isEmpty())) {
Object[] args = { GROUP_ID, groupId };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
List members = new ArrayList();
UserManager um = resourceResolver.adaptTo(UserManager.class);
Group reviewGroup = (Group) um.getAuthorizable(groupId);
if (reviewGroup == null) {
Object[] args = { GROUP_ID, groupId };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
Iterator memberIterator = reviewGroup
.getDeclaredMembers();
while (memberIterator.hasNext()) {
members.add(memberIterator.next().getID());
}
logger.trace("Exiting getGroupMembers.");
return members;
}
/**
* Returns the project sub tasks.
*
* @param reviewProjectName
* @param resourceResolver
* @return
* @throws FormsNDocumentsException
* @throws TaskManagerException
*/
public static List getProjectSubTasks(String reviewProjectName,
ResourceResolver resourceResolver) throws FormsNDocumentsException,
TaskManagerException {
logger.trace("Entering getProjectSubTasks.");
if (reviewProjectName == null
|| (reviewProjectName != null && reviewProjectName.isEmpty())) {
Object[] args = { PROJECT_NAME, reviewProjectName };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
List subTaskList = null;
TaskManager taskManager = resourceResolver.adaptTo(TaskManager.class);
Task task = taskManager.getTask(reviewProjectName, true);
if (task != null) {
subTaskList = task.getSubTasks();
}
logger.trace("Exiting getProjectSubTasks.");
return subTaskList;
}
/**
* Returns the resource resolver for the session.
*
* @param session
* @param resourceResolverFactory
* @return ResourceResolver
* @throws LoginException
*/
public static ResourceResolver getResourceResolver(Session session,
ResourceResolverFactory resourceResolverFactory)
throws LoginException {
logger.trace("Entering getResourceResolver.");
final Map authInfo = new HashMap(1);
authInfo.put(FMConstants.AUTHENTICATION_INFO_SESSION, session);
ResourceResolver resourceResolver = resourceResolverFactory
.getResourceResolver(authInfo);
logger.trace("Exiting getResourceResolver.");
return resourceResolver;
}
/**
* Returns the node for the given repository path.
*
* @param assetPath
* @param resourceResolver
* @return Node
* @throws FormsNDocumentsException
*/
public static Node getNode(String assetPath,
ResourceResolver resourceResolver) throws FormsNDocumentsException {
logger.trace("Entering getNode.");
if (assetPath == null || (assetPath != null && assetPath.isEmpty())) {
Object[] args = { CRX_PATH, assetPath };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
Resource resource = resourceResolver.getResource(assetPath);
logger.trace("Exiting getNode.");
if (resource == null) {
return null;
}
return resource.adaptTo(Node.class);
}
/**
* Removes the user group whose ID is given.
*
* @param groupName
* @param resourceResolver
* @throws FormsNDocumentsException
* @throws RepositoryException
*/
public static void removeGroup(String groupName,
ResourceResolver resourceResolver) throws FormsNDocumentsException,
RepositoryException {
logger.trace("Entering removeGroup.");
if (groupName == null || (groupName != null && groupName.isEmpty())) {
Object[] args = { GROUP_ID, groupName };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
UserManager um = resourceResolver.adaptTo(UserManager.class);
Authorizable authorizable = um.getAuthorizable(groupName);
authorizable.remove();
logger.trace("Exiting removeGroup.");
}
/**
* Creates a user group with the name provided.
*
* @param groupName
* @param resourceResolver
* @return
* @throws FormsNDocumentsException
* @throws RepositoryException
*/
public static Group createGroup(String groupName, String relativePath,
ResourceResolver resourceResolver) throws FormsNDocumentsException,
RepositoryException {
logger.trace("Entering createGroup.");
if (groupName == null || (groupName != null && groupName.isEmpty())) {
Object[] args = { GROUP_ID, groupName };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
Group group;
UserManager um = resourceResolver.adaptTo(UserManager.class);
if (relativePath == null || relativePath.isEmpty()) {
group = um.createGroup(groupName);
} else {
Principal principal = new PrincipalImpl(groupName);
group = um.createGroup(groupName, principal, relativePath);
}
logger.trace("Exiting createGroup.");
return group;
}
/**
* Adds the user as member of group.
*
* @param reviewer
* @param reviewGroupId
* @param resourceResolver
* @throws FormsNDocumentsException
* @throws RepositoryException
*/
public static void addUserToGroup(String reviewer, String reviewGroupId,
ResourceResolver resourceResolver) throws FormsNDocumentsException,
RepositoryException {
logger.trace("Entering addUserToGroup.");
if (reviewGroupId == null
|| (reviewGroupId != null && reviewGroupId.isEmpty())) {
Object[] args = { GROUP_ID, reviewGroupId };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
if (reviewer == null || (reviewer != null && reviewer.isEmpty())) {
Object[] args = { USER_ID, reviewer };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
UserManager um = resourceResolver.adaptTo(UserManager.class);
Authorizable authorizable = um.getAuthorizable(reviewer);
Group reviewGroup = (Group) um.getAuthorizable(reviewGroupId);
reviewGroup.addMember(authorizable);
logger.trace("Exiting addUserToGroup.");
}
/**
* Removes the member from group.
*
* @param reviewer
* @param reviewGroupId
* @param resourceResolver
* @throws FormsNDocumentsException
* @throws RepositoryException
*/
public static void removeUserFromGroup(String reviewer,
String reviewGroupId, ResourceResolver resourceResolver)
throws FormsNDocumentsException, RepositoryException {
logger.trace("Entering removeUserFromGroup.");
if (reviewGroupId == null
|| (reviewGroupId != null && reviewGroupId.isEmpty())) {
Object[] args = { GROUP_ID, reviewGroupId };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
if (reviewer == null || (reviewer != null && reviewer.isEmpty())) {
Object[] args = { USER_ID, reviewer };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
UserManager um = resourceResolver.adaptTo(UserManager.class);
Authorizable authorizable = um.getAuthorizable(reviewer);
Group reviewGroup = (Group) um.getAuthorizable(reviewGroupId);
reviewGroup.removeMember(authorizable);
logger.trace("Exiting removeUserFromGroup.");
}
/**
* Creates the Task of type Project
*
* @param reviewName
* @param assetPath
* @param deadline
* @param assignee
* @param bundleResourceResolver
* @param resourceResolver
* @return
* @throws FormsNDocumentsException
* @throws TaskManagerException
*/
public static Task createProject(String reviewName, String assetPath,
String deadline, String assignee, ResourceResolver bundleResourceResolver, ResourceResolver resourceResolver)
throws FormsNDocumentsException, TaskManagerException {
logger.trace("Entering createProject.");
String reviewTasksRootPath = assetPath + REVIEW_TASKS_PATH;
checkAssetPathArgument(reviewTasksRootPath);
checkCreateReviewArguments(reviewName, new String[] { assignee },
deadline, bundleResourceResolver);
Task task;
Resource assetResource = resourceResolver.getResource(reviewTasksRootPath);
TaskManager taskManager = assetResource.adaptTo(TaskManager.class);
task = taskManager.getTaskManagerFactory().newTask(PROJECT_TYPE);
task.setName(reviewName);
task.setDescription(reviewName);
task.setContentPath(assetPath);
task.setProperty(TASK_DUE_DATE, deadline);
task = taskManager.createTask(task);
logger.trace("Exiting createProject.");
return task;
}
/**
* Completes the Project if its Active. Terminates all its incomplete tasks.
* This method only works for Projects whose status is 'ACTIVE'.
*
* @param reviewProjectName
* @param resourceResolver
* @throws FormsNDocumentsException
* @throws TaskManagerException
*/
public static void terminateProjectActiveTasks(String reviewProjectName,
ResourceResolver resourceResolver) throws FormsNDocumentsException,
TaskManagerException {
logger.trace("Entering terminateProjectActiveTasks.");
if (reviewProjectName == null
|| (reviewProjectName != null && reviewProjectName.isEmpty())) {
Object[] args = { PROJECT_NAME, reviewProjectName };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
TaskManager taskManager = resourceResolver.adaptTo(TaskManager.class);
Task task = taskManager.getTask(reviewProjectName, true);
if (task != null && Status.ACTIVE.equals(task.getStatus())) {
List subTaskList = task.getSubTasks();
if (subTaskList != null) {
for (Task subTask : subTaskList) {
if (isTaskEligibleForUpdate(subTask.getStatus())) {
taskManager.terminateTask(subTask.getId());
}
}
}
taskManager.completeTask(task.getId(), null);
}
logger.trace("Exiting terminateProjectActiveTasks.");
}
/**
*
*
* @param reviewProjectName
* @param deadline
* @param slingRepository
* @param resourceResolverFactory
* @throws TaskManagerException
* @throws RepositoryException
* @throws LoginException
* @throws FormsNDocumentsException
*/
/**
* Updates the deadline of Project if its not already completed. Updates the
* deadline of all its incomplete Tasks.
*
* @param reviewProjectName
* @param deadline
* @param resourceResolver
* @throws FormsNDocumentsException
* @throws TaskManagerException
*/
public static void updateProjectDeadline(String reviewProjectName,
String deadline, ResourceResolver resourceResolver)
throws FormsNDocumentsException, TaskManagerException {
logger.trace("Entering updateProjectDeadline.");
checkReviewDeadlineArgument(deadline);
TaskManager taskManager = resourceResolver.adaptTo(TaskManager.class);
Task task = taskManager.getTask(reviewProjectName, true);
if (task != null && Status.ACTIVE.equals(task.getStatus())) {
task.setProperty(TASK_DUE_DATE, deadline);
taskManager.saveTask(task);
List subTaskList = task.getSubTasks();
if (subTaskList != null) {
for (Task subTask : subTaskList) {
if (isTaskEligibleForUpdate(subTask.getStatus())) {
subTask.setProperty(TASK_DUE_DATE, deadline);
taskManager.saveTask(subTask);
}
}
}
}
logger.trace("Exiting updateProjectDeadline.");
}
/**
* Updates the description of Project if its not already completed. Updates
* the deadline of all its incomplete Tasks.
*
* @param reviewProjectName
* @param description
* @param resourceResolver
* @throws TaskManagerException
*/
public static void updateProjectDescription(String reviewProjectName,
String description, ResourceResolver resourceResolver)
throws TaskManagerException {
logger.trace("Entering updateProjectDescription.");
if (description == null) {
return;
}
TaskManager taskManager = resourceResolver.adaptTo(TaskManager.class);
Task task = taskManager.getTask(reviewProjectName, true);
if (task != null && Status.ACTIVE.equals(task.getStatus())) {
task.setDescription(description);
taskManager.saveTask(task);
List subTaskList = task.getSubTasks();
if (subTaskList != null) {
for (Task subTask : subTaskList) {
if (isTaskEligibleForUpdate(subTask.getStatus())) {
subTask.setDescription(description);
taskManager.saveTask(subTask);
}
}
}
}
logger.trace("Exiting updateProjectDescription.");
}
/**
* Creates a Task with given attribute
*
* @param reviewProjectName
* @param reviewName
* @param deadline
* @param reviewer
* @param assetPath
* @param reviewDescription
* @param bundleResourceResolver
* @param resourceResolver
* @return
* @throws FormsNDocumentsException
* @throws TaskManagerException
*/
public static Task createTask(String reviewProjectName, String reviewName,
String deadline, String reviewer, String assetPath,
String reviewDescription, ResourceResolver bundleResourceResolver, ResourceResolver resourceResolver)
throws FormsNDocumentsException, TaskManagerException {
logger.trace("Entering createTask.");
checkCreateReviewArguments(reviewName, new String[] { reviewer },
deadline, bundleResourceResolver);
Task task;
String reviewTasksRootPath = assetPath + REVIEW_TASKS_PATH;
Resource assetResource = resourceResolver.getResource(reviewTasksRootPath);
TaskManager taskManager = assetResource.adaptTo(TaskManager.class);
task = taskManager.getTaskManagerFactory().newTask(PROJECT_SUB_TYPE);
task.setName(reviewName);
task.setDescription(reviewDescription);
task.setCurrentAssignee(reviewer);
task.setContentPath(assetPath);
task.setProperty(TASK_DUE_DATE, deadline);
task = taskManager.createTask(reviewProjectName, task);
logger.trace("Exiting createTask.");
return task;
}
/**
* Checks whether the given id is in the given array.
*
* @param searchId
* @param idArray
* @return boolean
*/
public static boolean isInArray(String searchId, String[] idArray) {
logger.trace("Entering isInArray.");
if (searchId != null && idArray != null && idArray.length > 0) {
for (String id : idArray) {
if (searchId.equals(id)) {
logger.trace("Exiting isInArray.");
return true;
}
}
}
logger.trace("Exiting isInArray.");
return false;
}
/**
* Checks whether the arguments passed for review creation are valid or not.
* If not, it throws a FormsNDocumentsException exception.
*
* @param reviewName
* @param reviewerList
* @param deadline
* @param resourceResolver
* @throws FormsNDocumentsException
*/
public static void checkCreateReviewArguments(String reviewName,
String[] reviewerList, String deadline,
ResourceResolver resourceResolver) throws FormsNDocumentsException {
logger.trace("Entering checkCreateReviewArguments.");
// Mandatory
if (reviewName == null || (reviewName != null && reviewName.isEmpty())) {
Object[] args = { REVIEW_NAME, reviewName };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
checkUpdateReviewArguments(reviewerList, deadline, resourceResolver);
logger.trace("Exiting checkCreateReviewArguments.");
}
/**
* Checks whether the arguments passed for review updation are valid or not.
* If not, it throws a FormsNDocumentsException exception.
*
* @param reviewerList
* @param deadline
* @param bundleResourceResolver fnd-service system user resource resolver
* @throws FormsNDocumentsException
*/
public static void checkUpdateReviewArguments(String[] reviewerList,
String deadline, ResourceResolver bundleResourceResolver)
throws FormsNDocumentsException {
logger.trace("Entering checkUpdateReviewArguments.");
// Mandatory
if (reviewerList == null
|| (reviewerList != null && reviewerList.length < 1)) {
Object[] args = { REVIEWERS, reviewerList };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
} else {
String reviewerId = "";
try {
UserManager um = bundleResourceResolver.adaptTo(UserManager.class);
for (String reviewer : reviewerList) {
reviewerId = reviewer;
Authorizable authorizable = um.getAuthorizable(reviewer);
if (authorizable == null) {
Object[] args = { reviewer };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_021,
args);
}
}
} catch (RepositoryException e) {
logger.error("User with id " + reviewerId + " doesn't exist " , e);
Object[] args = { reviewerId };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_021, args);
}
}
checkReviewDeadlineArgument(deadline);
logger.trace("Exiting checkUpdateReviewArguments.");
}
/**
* Checks whether the deadline argument passed for review updation is valid
* or not. If not, it throws a FormsNDocumentsException exception.
*
* @param deadline
* @throws FormsNDocumentsException
*/
public static void checkReviewDeadlineArgument(String deadline)
throws FormsNDocumentsException {
logger.trace("Entering checkReviewDeadlineArgument.");
// Greater than now
if (deadline != null && !deadline.isEmpty()) {
try {
Date deadlineDate = sdf.parse(deadline);
Date now = new Date();
if (deadlineDate.compareTo(now) <= 0) {
Object[] args = null;
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_024,args);
}
} catch (ParseException e) {
logger.error("Exception while parsing review deadline date format" , e);
Object[] args = { REVIEW_DEADLINE, deadline };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
}
logger.trace("Exiting checkReviewDeadlineArgument.");
}
/**
* Checks whether the asset path passed as argument is valid or not. If not,
* it throws a FormsNDocumentsException exception.
*
* @param assetPath
* @throws FormsNDocumentsException
*/
public static void checkAssetPathArgument(String assetPath)
throws FormsNDocumentsException {
logger.trace("Entering checkAssetPathArgument.");
// Mandatory
if (assetPath == null || (assetPath != null && assetPath.isEmpty())) {
Object[] args = { ASSET_PATH, assetPath };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_001, args);
}
logger.trace("Exiting checkAssetPathArgument.");
}
/**
* Checks whether the node exist on asset path passed as argument or not. If
* not, it throws a FormsNDocumentsException exception.
*
* @param assetPath
* @param resourceResolver
* @return Node
* @throws FormsNDocumentsException
*/
public static Node checkNodeExistance(String assetPath,
ResourceResolver resourceResolver) throws FormsNDocumentsException {
logger.trace("Entering checkNodeExsistance.");
Node asset = getNode(assetPath, resourceResolver);
if (asset == null) {
Object[] args = { assetPath };
throw new FormsNDocumentsException(
FormsNDocumentsException.ERROR_AEM_FMG_700_002, args);
}
logger.trace("Exiting checkNodeExsistance.");
return asset;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy