ListItem.java 16.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.security.user.UserProperties
 *  com.adobe.granite.security.user.UserPropertiesManager
 *  com.adobe.granite.taskmanagement.Task
 *  com.adobe.granite.taskmanagement.TaskAction
 *  com.adobe.granite.workflow.exec.InboxItem
 *  com.adobe.granite.workflow.exec.Status
 *  com.adobe.granite.workflow.payload.PayloadInfo
 *  com.adobe.granite.workflow.payload.PayloadInfoBuilderManager
 *  com.day.cq.commons.TidyJSONWriter
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.lang.StringUtils
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.jackrabbit.api.security.user.UserManager
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.JSONException
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.projects.impl.servlet;

import com.adobe.cq.projects.impl.servlet.ProjectServletUtil;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.adobe.granite.taskmanagement.Task;
import com.adobe.granite.taskmanagement.TaskAction;
import com.adobe.granite.workflow.exec.InboxItem;
import com.adobe.granite.workflow.exec.Status;
import com.adobe.granite.workflow.payload.PayloadInfo;
import com.adobe.granite.workflow.payload.PayloadInfoBuilderManager;
import com.day.cq.commons.TidyJSONWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class ListItem {
    private static final Logger LOGGER = LoggerFactory.getLogger(ListItem.class);
    public static final String NO_WORKFLOW_INSTANCE = "_no_workflow_instance_";
    public static final String PROP_NAME_WFINSTANCE_ID = "wfInstanceId";
    private final String mapId;

    public ListItem(String mapId) {
        this.mapId = mapId;
    }

    public String getMapId() {
        return this.mapId;
    }

    public abstract void write(TidyJSONWriter var1, UserManager var2, UserPropertiesManager var3, ResourceResolver var4, SlingHttpServletRequest var5, HttpServletResponse var6, Map<String, String> var7) throws JSONException, ServletException, IOException;

    public abstract Date getDueDate();

    public abstract boolean isActive();

    public int countSubItems() {
        return 0;
    }

    protected void writeTask(ResourceResolver resolver, UserManager usrMgr, UserPropertiesManager userPropertiesManager, TidyJSONWriter writer, Task inboxItem, SlingHttpServletRequest slingRequest, HttpServletResponse response, boolean isTaskGroupMember, boolean isRootTask, boolean isLastGroupMember, Map<String, String> config) throws JSONException, ServletException, IOException {
        writer.key("item").value((Object)inboxItem.getId());
        writer.key("title").value((Object)inboxItem.getName());
        writer.key("description").value((Object)inboxItem.getDescription());
        this.writeCurrentAssignee(usrMgr, userPropertiesManager, writer, inboxItem.getCurrentAssignee());
        writer.key("startTime").value(inboxItem.getTimeStarted().getTime());
        String computedInboxType = this.getComputedInboxType((InboxItem)inboxItem);
        writer.key("actionNames");
        writer.array();
        List taskActions = inboxItem.getActions();
        if (taskActions != null) {
            for (TaskAction action : taskActions) {
                writer.value((Object)action.getActionID());
            }
        }
        writer.endArray();
        writer.key("taskInfo");
        this.writeTaskInfo(slingRequest.getResourceResolver(), writer, inboxItem, config);
        if (isTaskGroupMember) {
            writer.key("isGroupRoot").value(isRootTask);
            writer.key("isLastGroupMember").value(isLastGroupMember);
            writer.key("inboxItemType").value((Object)"TaskGroupMember");
        } else {
            writer.key("inboxItemType").value((Object)"ProjectTask");
        }
        writer.key("computedItemType").value((Object)this.firstUpper(computedInboxType));
        String payloadType = this.getPayloadType((InboxItem)inboxItem);
        if (inboxItem.getContentPath() != null) {
            this.writePayloadObject(resolver, writer, (InboxItem)inboxItem, payloadType);
            String comment = this.getComment((InboxItem)inboxItem);
            if (comment != null && !comment.equals("")) {
                writer.key("comment").value((Object)comment);
            }
        }
    }

    private String firstUpper(String taskTypeName) {
        if (taskTypeName == null || taskTypeName.length() < 1) {
            return taskTypeName;
        }
        if (taskTypeName.length() < 2) {
            return taskTypeName.toUpperCase();
        }
        return "" + Character.toUpperCase(taskTypeName.charAt(0)) + taskTypeName.substring(1);
    }

    private String getComment(InboxItem inboxItem) {
        Task task;
        String comment = null;
        if (inboxItem instanceof Task && (task = (Task)inboxItem).getProperty("comment") instanceof String) {
            comment = (String)task.getProperty("comment");
        }
        return comment;
    }

    protected void writePayloadObject(ResourceResolver resolver, TidyJSONWriter writer, InboxItem item, String payloadType) {
        block11 : {
            if ("JCR_PATH".equals(payloadType)) {
                try {
                    PayloadInfoBuilderManager payloadBuilder = (PayloadInfoBuilderManager)resolver.adaptTo(PayloadInfoBuilderManager.class);
                    PayloadInfo info = payloadBuilder.getPayloadInfo(item, null);
                    if (StringUtils.isNotBlank((String)info.getBrowserPath())) {
                        writer.key("payload").value((Object)info.getBrowserPath());
                    } else {
                        writer.key("payload").value((Object)item.getContentPath());
                    }
                    String payloadPath = info.getPath();
                    Resource res = resolver.getResource(payloadPath);
                    if (res != null) {
                        writer.key("payloadPath").value((Object)payloadPath);
                        this.writePayloadSummary(writer, info);
                        String payloadTitle = info.getTitle();
                        if (payloadTitle != null) {
                            writer.key("payloadTitle").value((Object)payloadTitle);
                        }
                        break block11;
                    }
                    LOGGER.error("Payload not found for work item: " + this.mapId + " (" + payloadPath + ")");
                }
                catch (Exception e) {
                    LOGGER.error("Unable to process inboxitem: " + this.mapId, (Throwable)e);
                }
            } else if ("URL".equals(payloadType)) {
                try {
                    String payloadUrl = item.getContentPath();
                    writer.key("payload").value((Object)payloadUrl);
                    writer.key("payloadPath").value((Object)payloadUrl);
                    writer.key("payloadTitle").value((Object)payloadUrl);
                    writer.key("payloadType").value((Object)"URL");
                }
                catch (Exception e) {
                    LOGGER.error("Unable to process inboxitem: " + this.mapId, (Throwable)e);
                }
            }
        }
    }

    private void writePayloadSummary(TidyJSONWriter out, PayloadInfo info) throws JSONException {
        out.key("payloadSummary").object();
        if (StringUtils.isNotBlank((String)info.getDescription())) {
            out.key("description").value((Object)info.getDescription());
        }
        if (StringUtils.isNotBlank((String)info.getThumbnailPath())) {
            out.key("icon").value((Object)info.getThumbnailPath());
        }
        out.endObject();
    }

    protected void writeCurrentAssignee(UserManager usrMgr, UserPropertiesManager userPropertiesManager, TidyJSONWriter writer, String currAssignee) throws JSONException {
        String currentAssigneeType = "user";
        if (currAssignee != null) {
            Authorizable auth = null;
            UserProperties up = null;
            try {
                auth = usrMgr.getAuthorizable(currAssignee);
                if (auth != null && (up = userPropertiesManager.getUserProperties(auth, "profile")) != null) {
                    String alternateTitle = up.getProperty("alternateTitle");
                    if (StringUtils.isNotBlank((String)alternateTitle)) {
                        currAssignee = alternateTitle;
                    } else {
                        String newAssignee = up.getDisplayName();
                        if (!StringUtils.isBlank((String)newAssignee)) {
                            currAssignee = newAssignee;
                        }
                    }
                }
            }
            catch (RepositoryException e) {
                // empty catch block
            }
            String thumbnailPath = "/libs/granite/security/clientlib/themes/default/resources/sample-user-thumbnail.36.png";
            if (auth != null) {
                thumbnailPath = ProjectServletUtil.getUserThumbnailPath(auth, up);
                if (auth.isGroup()) {
                    currentAssigneeType = "group";
                }
            }
            writer.key("participant").value((Object)currAssignee);
            writer.key("currentAssigneeType").value((Object)currentAssigneeType);
            writer.key("currentAssigneeThumbnail").value((Object)thumbnailPath);
            writer.key("currentAssignee").value((Object)currAssignee);
        }
    }

    private String getComputedInboxType(InboxItem inboxItem) {
        Task task;
        String computedInboxType = inboxItem.getItemType();
        if (inboxItem instanceof Task && (task = (Task)inboxItem).getTaskTypeName() != null) {
            computedInboxType = task.getTaskTypeName();
        }
        return computedInboxType;
    }

    private String getPayloadType(InboxItem inboxItem) {
        if (inboxItem instanceof Task) {
            return "JCR_PATH";
        }
        return null;
    }

    private void writeTaskInfo(ResourceResolver resourceResolver, TidyJSONWriter w, Task task, Map<String, String> taskTypeContentMapping) throws JSONException {
        w.object();
        w.key("id").value((Object)task.getId());
        w.key("parentTaskId").value((Object)task.getParentId());
        w.key("actionNames");
        w.array();
        List taskActions = task.getActions();
        if (taskActions != null) {
            for (TaskAction action : taskActions) {
                w.value((Object)action.getActionID());
            }
        }
        w.endArray();
        Calendar calendar = Calendar.getInstance();
        if (task.getLastModified() != null) {
            calendar.setTime(task.getLastModified());
            this.writeCalendar(w, "updateTime", calendar);
        }
        if (task.getTimeStarted() != null) {
            calendar.setTime(task.getTimeStarted());
            this.writeCalendar(w, "createTime", calendar);
        }
        if (task.getTimeEnded() != null) {
            calendar.setTime(task.getTimeEnded());
            this.writeCalendar(w, "completeTime", calendar);
        }
        w.key("description").value((Object)task.getDescription());
        w.key("instructions").value((Object)task.getInstructions());
        w.key("contentPath").value((Object)task.getContentPath());
        w.key("hasSubTasks").value(task.hasSubTasks());
        w.key("name").value((Object)task.getName());
        w.key("ownerId").value((Object)task.getCurrentAssignee());
        w.key("createdBy").value((Object)task.getCreatedBy());
        String selectedActionId = null;
        if (task.getSelectedAction() != null) {
            selectedActionId = task.getSelectedAction().getActionID();
        }
        w.key("selectedAction").value((Object)selectedActionId);
        w.key("properties");
        boolean hasWorkflowInstanceId = false;
        w.object();
        Iterator propertyNameIterator = task.getPropertyNames();
        if (propertyNameIterator != null) {
            while (propertyNameIterator.hasNext()) {
                Object propertyValue;
                String key = (String)propertyNameIterator.next();
                if ("wfInstanceId".equals(key)) {
                    hasWorkflowInstanceId = true;
                }
                if ((propertyValue = task.getProperty(key)) == null) continue;
                w.key(key).value(propertyValue);
            }
        }
        if (!hasWorkflowInstanceId) {
            w.key("wfInstanceId").value((Object)"_no_workflow_instance_");
        }
        w.endObject();
        w.key("status").value((Object)task.getStatus());
        w.key("taskType").value((Object)task.getTaskTypeName());
        w.key("writeAccess").value(this.hasWriteAccess(resourceResolver, task));
        if (taskTypeContentMapping != null) {
            w.key("url").value((Object)taskTypeContentMapping.get(task.getTaskTypeName()));
        }
        w.endObject();
    }

    private void writeCalendar(TidyJSONWriter out, String key, Calendar value) throws JSONException {
        if (value != null) {
            out.key(key).value(value.getTimeInMillis());
        }
    }

    private boolean hasWriteAccess(ResourceResolver resourceResolver, Task task) {
        Session session = (Session)resourceResolver.adaptTo(Session.class);
        if (task.getCurrentAssignee().equals(session.getUserID())) {
            return true;
        }
        try {
            return session.hasPermission(task.getId(), "set_property");
        }
        catch (RepositoryException e) {
            LOGGER.error("error checking access for task: " + task.getId(), (Throwable)e);
            return false;
        }
    }

    protected Date parseDate(String dueDateString) {
        if (StringUtils.isBlank((String)dueDateString)) {
            return null;
        }
        SimpleDateFormat formatterWithSeconds = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            return formatterWithSeconds.parse(dueDateString);
        }
        catch (ParseException e) {
            SimpleDateFormat formatterWithoutSeconds = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            try {
                return formatterWithoutSeconds.parse(dueDateString);
            }
            catch (ParseException e2) {
                LOGGER.debug("Failed to parse date {}", (Object)dueDateString, (Object)e2);
                return null;
            }
        }
    }

    protected Date getTaskDueDate(Task task) {
        Object dueDate;
        if (task != null && (dueDate = task.getProperty("taskDueDate")) != null) {
            if (dueDate instanceof String) {
                return this.parseDate((String)dueDate);
            }
            if (dueDate instanceof Date) {
                LOGGER.debug("task due date is a date, value '{}'", dueDate);
                return (Date)dueDate;
            }
            LOGGER.debug("task due date is not of type 'date', instead it is {}", (Object)dueDate.getClass().getSimpleName());
        }
        return null;
    }

    public void sortTasks() {
    }

    public int compareDueDates(ListItem listItem) {
        int statusComparison = this.compareStatusWith(listItem);
        if (statusComparison != 0) {
            return statusComparison;
        }
        Date leftDueDate = this.getDueDate();
        Date rightDueDate = listItem.getDueDate();
        if (leftDueDate != null) {
            if (rightDueDate != null) {
                return leftDueDate.compareTo(rightDueDate);
            }
            return -1;
        }
        if (rightDueDate != null) {
            return 1;
        }
        return 0;
    }

    protected int compareStatusWith(ListItem listItem) {
        if (this.isActive()) {
            if (listItem.isActive()) {
                return 0;
            }
            return -1;
        }
        if (listItem.isActive()) {
            return 1;
        }
        return 0;
    }
}