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 @Override 061 public String getI18nTaskName() { 062 if (locale == null) { 063 return getName(); 064 } 065 066 String labelKey = getName(); 067 if (needi18n()) { 068 labelKey = "label.workflow.task." + labelKey; 069 } 070 return getI18nLabel(labelKey, locale); 071 } 072 073 @Override 074 public String getI18nDirective() { 075 String directiveKey = getDirective(); 076 if (directiveKey == null) { 077 directiveKey = getName(); 078 } 079 if (locale == null) { 080 return directiveKey; 081 } 082 083 String directiveLabel = getI18nLabel(directiveKey, locale); 084 if (directiveKey != null && directiveKey.equals(directiveLabel)) { 085 if (needi18n()) { 086 directiveKey = "label.workflow.task." + directiveKey; 087 } 088 String newdirectiveLabel = getI18nLabel(directiveKey, locale); 089 if (!directiveKey.equals(newdirectiveLabel)) { 090 directiveLabel = newdirectiveLabel; 091 } 092 } 093 return directiveLabel; 094 } 095 096 protected boolean isCreatedFromCreateTaskOperation() { 097 return Boolean.parseBoolean(getTask().getVariable("createdFromCreateTaskOperation")); 098 } 099 100 protected boolean needi18n() { 101 if (isCreatedFromCreateTaskOperation()) { 102 return false; 103 } 104 return Boolean.parseBoolean(getTask().getVariable(Task.TaskVariableName.needi18n.name())); 105 } 106 107 @Override 108 public Map<String, Object> asMap() { 109 110 isCreatedFromCreateTaskOperation(); 111 112 Map<String, Object> obj = new LinkedHashMap<>(); 113 obj.put("id", getTask().getId()); 114 obj.put("docref", getDocument().getRef().toString()); 115 obj.put("name", getName()); 116 obj.put("taskName", getI18nTaskName()); 117 obj.put("directive", getI18nDirective()); 118 String comment = getComment(); 119 obj.put("comment", comment != null ? comment : ""); 120 Date dueDate = getDueDate(); 121 obj.put("dueDate", dueDate != null ? DateParser.formatW3CDateTime(dueDate) : ""); 122 obj.put("documentTitle", getDocument().getTitle()); 123 obj.put("documentLink", getDocumentLink(false)); 124 Date startDate = getStartDate(); 125 obj.put("startDate", startDate != null ? DateParser.formatW3CDateTime(startDate) : ""); 126 boolean expired = false; 127 if (dueDate != null) { 128 expired = dueDate.before(new Date()); 129 } 130 obj.put("expired", expired); 131 return obj; 132 133 } 134 135 @Override 136 public String getDocumentLink(boolean includeWorkflowTab) { 137 DocumentModel doc = getDocument(); 138 DocumentViewCodecManager documentViewCodecManager = Framework.getService(DocumentViewCodecManager.class); 139 if (documentViewCodecManager != null) { 140 String viewId = getDefaultViewFor(doc); 141 Map<String, String> parameters = new HashMap<>(); 142 if (includeWorkflowTab) { 143 parameters.put("tabId", "TAB_CONTENT_JBPM"); 144 } 145 DocumentView docView = new DocumentViewImpl(new DocumentLocationImpl(doc), viewId, parameters); 146 return documentViewCodecManager.getUrlFromDocumentView("docpath", docView, false, null); 147 } else { 148 return ""; 149 } 150 } 151 152 protected String getDefaultViewFor(DocumentModel doc) { 153 TypeInfo type = doc.getAdapter(TypeInfo.class); 154 if (type == null) { 155 return null; 156 } 157 return type.getDefaultView(); 158 } 159 160 public Locale getLocale() { 161 return locale; 162 } 163 164 @Override 165 public void setLocale(Locale locale) { 166 this.locale = locale; 167 } 168 169}