001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 */ 018package org.nuxeo.ecm.platform.task.dashboard; 019 020import java.util.Date; 021import java.util.HashMap; 022import java.util.LinkedHashMap; 023import java.util.Locale; 024import java.util.Map; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.common.utils.i18n.I18NUtils; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl; 031import org.nuxeo.ecm.core.schema.utils.DateParser; 032import org.nuxeo.ecm.platform.task.Task; 033import org.nuxeo.ecm.platform.types.adapter.TypeInfo; 034import org.nuxeo.ecm.platform.url.DocumentViewImpl; 035import org.nuxeo.ecm.platform.url.api.DocumentView; 036import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager; 037import org.nuxeo.runtime.api.Framework; 038 039/** 040 * @since 5.5 041 */ 042public abstract class AbstractDashBoardItemImpl implements DashBoardItem { 043 044 private static final long serialVersionUID = 1L; 045 046 protected static Log log = LogFactory.getLog(AbstractDashBoardItemImpl.class); 047 048 protected Locale locale; 049 050 protected String getI18nLabel(String label, Locale locale) { 051 if (label == null) { 052 label = ""; 053 } 054 if (locale == null) { 055 return label; 056 } 057 return I18NUtils.getMessageString("messages", label, null, locale); 058 } 059 060 public String getI18nTaskName() { 061 if (locale == null) { 062 return getName(); 063 } 064 065 String labelKey = getName(); 066 if (needi18n()) { 067 labelKey = "label.workflow.task." + labelKey; 068 } 069 return getI18nLabel(labelKey, locale); 070 } 071 072 public String getI18nDirective() { 073 String directiveKey = getDirective(); 074 if (directiveKey == null) { 075 directiveKey = getName(); 076 } 077 if (locale == null) { 078 return directiveKey; 079 } 080 081 String directiveLabel = getI18nLabel(directiveKey, locale); 082 if (directiveKey != null && directiveKey.equals(directiveLabel)) { 083 if (needi18n()) { 084 directiveKey = "label.workflow.task." + directiveKey; 085 } 086 String newdirectiveLabel = getI18nLabel(directiveKey, locale); 087 if (!directiveKey.equals(newdirectiveLabel)) { 088 directiveLabel = newdirectiveLabel; 089 } 090 } 091 return directiveLabel; 092 } 093 094 protected boolean isCreatedFromCreateTaskOperation() { 095 return Boolean.parseBoolean(getTask().getVariable("createdFromCreateTaskOperation")); 096 } 097 098 protected boolean needi18n() { 099 if (isCreatedFromCreateTaskOperation()) { 100 return false; 101 } 102 return Boolean.parseBoolean(getTask().getVariable(Task.TaskVariableName.needi18n.name())); 103 } 104 105 public Map<String, Object> asMap() { 106 107 boolean createdFromCreateTaskOperation = isCreatedFromCreateTaskOperation(); 108 109 Map<String, Object> obj = new LinkedHashMap<>(); 110 obj.put("id", getTask().getId()); 111 obj.put("docref", getDocument().getRef().toString()); 112 obj.put("name", getName()); 113 obj.put("taskName", getI18nTaskName()); 114 obj.put("directive", getI18nDirective()); 115 String comment = getComment(); 116 obj.put("comment", comment != null ? comment : ""); 117 Date dueDate = getDueDate(); 118 obj.put("dueDate", dueDate != null ? DateParser.formatW3CDateTime(dueDate) : ""); 119 obj.put("documentTitle", getDocument().getTitle()); 120 obj.put("documentLink", getDocumentLink(false)); 121 Date startDate = getStartDate(); 122 obj.put("startDate", startDate != null ? DateParser.formatW3CDateTime(startDate) : ""); 123 boolean expired = false; 124 if (dueDate != null) { 125 expired = dueDate.before(new Date()); 126 } 127 obj.put("expired", expired); 128 return obj; 129 130 } 131 132 public String getDocumentLink(boolean includeWorkflowTab) { 133 DocumentModel doc = getDocument(); 134 DocumentViewCodecManager documentViewCodecManager = Framework.getService(DocumentViewCodecManager.class); 135 if (documentViewCodecManager != null) { 136 String viewId = getDefaultViewFor(doc); 137 Map<String, String> parameters = new HashMap<String, String>(); 138 if (includeWorkflowTab) { 139 parameters.put("tabId", "TAB_CONTENT_JBPM"); 140 } 141 DocumentView docView = new DocumentViewImpl(new DocumentLocationImpl(doc), viewId, parameters); 142 return documentViewCodecManager.getUrlFromDocumentView("docpath", docView, false, null); 143 } else { 144 return ""; 145 } 146 } 147 148 protected String getDefaultViewFor(DocumentModel doc) { 149 TypeInfo type = doc.getAdapter(TypeInfo.class); 150 if (type == null) { 151 return null; 152 } 153 return type.getDefaultView(); 154 } 155 156 public Locale getLocale() { 157 return locale; 158 } 159 160 public void setLocale(Locale locale) { 161 this.locale = locale; 162 } 163 164}