001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 */
016package org.nuxeo.ecm.platform.task.dashboard;
017
018import java.util.Date;
019import java.util.HashMap;
020import java.util.Locale;
021import java.util.Map;
022
023import net.sf.json.JSONObject;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.common.utils.i18n.I18NUtils;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
030import org.nuxeo.ecm.core.schema.utils.DateParser;
031import org.nuxeo.ecm.platform.task.Task;
032import org.nuxeo.ecm.platform.types.adapter.TypeInfo;
033import org.nuxeo.ecm.platform.url.DocumentViewImpl;
034import org.nuxeo.ecm.platform.url.api.DocumentView;
035import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * @since 5.5
040 */
041public abstract class AbstractDashBoardItemImpl implements DashBoardItem {
042
043    private static final long serialVersionUID = 1L;
044
045    protected static Log log = LogFactory.getLog(AbstractDashBoardItemImpl.class);
046
047    protected Locale locale;
048
049    protected String getI18nLabel(String label, Locale locale) {
050        if (label == null) {
051            label = "";
052        }
053        if (locale == null) {
054            return label;
055        }
056        return I18NUtils.getMessageString("messages", label, null, locale);
057    }
058
059    public String getI18nTaskName() {
060        if (locale == null) {
061            return getName();
062        }
063
064        String labelKey = getName();
065        if (needi18n()) {
066            labelKey = "label.workflow.task." + labelKey;
067        }
068        return getI18nLabel(labelKey, locale);
069    }
070
071    public String getI18nDirective() {
072        String directiveKey = getDirective();
073        if (directiveKey == null) {
074            directiveKey = getName();
075        }
076        if (locale == null) {
077            return directiveKey;
078        }
079
080        String directiveLabel = getI18nLabel(directiveKey, locale);
081        if (directiveKey != null && directiveKey.equals(directiveLabel)) {
082            if (needi18n()) {
083                directiveKey = "label.workflow.task." + directiveKey;
084            }
085            String newdirectiveLabel = getI18nLabel(directiveKey, locale);
086            if (!directiveKey.equals(newdirectiveLabel)) {
087                directiveLabel = newdirectiveLabel;
088            }
089        }
090        return directiveLabel;
091    }
092
093    protected boolean isCreatedFromCreateTaskOperation() {
094        return Boolean.parseBoolean(getTask().getVariable("createdFromCreateTaskOperation"));
095    }
096
097    protected boolean needi18n() {
098        if (isCreatedFromCreateTaskOperation()) {
099            return false;
100        }
101        return Boolean.parseBoolean(getTask().getVariable(Task.TaskVariableName.needi18n.name()));
102    }
103
104    public JSONObject asJSON() {
105
106        boolean createdFromCreateTaskOperation = isCreatedFromCreateTaskOperation();
107
108        JSONObject obj = new JSONObject();
109        obj.put("id", getTask().getId());
110        obj.put("docref", getDocument().getRef().toString());
111        obj.put("name", getName());
112        obj.put("taskName", getI18nTaskName());
113        obj.put("directive", getI18nDirective());
114        String comment = getComment();
115        obj.put("comment", comment != null ? comment : "");
116        Date dueDate = getDueDate();
117        obj.put("dueDate", dueDate != null ? DateParser.formatW3CDateTime(dueDate) : "");
118        obj.put("documentTitle", getDocument().getTitle());
119        obj.put("documentLink", getDocumentLink(false));
120        Date startDate = getStartDate();
121        obj.put("startDate", startDate != null ? DateParser.formatW3CDateTime(startDate) : "");
122        boolean expired = false;
123        if (dueDate != null) {
124            expired = dueDate.before(new Date());
125        }
126        obj.put("expired", expired);
127        return obj;
128
129    }
130
131    public String getDocumentLink(boolean includeWorkflowTab) {
132        DocumentModel doc = getDocument();
133        DocumentViewCodecManager documentViewCodecManager = Framework.getLocalService(DocumentViewCodecManager.class);
134        if (documentViewCodecManager != null) {
135            String viewId = getDefaultViewFor(doc);
136            Map<String, String> parameters = new HashMap<String, String>();
137            if (includeWorkflowTab) {
138                parameters.put("tabId", "TAB_CONTENT_JBPM");
139            }
140            DocumentView docView = new DocumentViewImpl(new DocumentLocationImpl(doc), viewId, parameters);
141            return documentViewCodecManager.getUrlFromDocumentView("docpath", docView, false, null);
142        } else {
143            return "";
144        }
145    }
146
147    protected String getDefaultViewFor(DocumentModel doc) {
148        TypeInfo type = doc.getAdapter(TypeInfo.class);
149        if (type == null) {
150            return null;
151        }
152        return type.getDefaultView();
153    }
154
155    public Locale getLocale() {
156        return locale;
157    }
158
159    public void setLocale(Locale locale) {
160        this.locale = locale;
161    }
162
163}