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.Locale;
023import java.util.Map;
024
025import net.sf.json.JSONObject;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.utils.i18n.I18NUtils;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
032import org.nuxeo.ecm.core.schema.utils.DateParser;
033import org.nuxeo.ecm.platform.task.Task;
034import org.nuxeo.ecm.platform.types.adapter.TypeInfo;
035import org.nuxeo.ecm.platform.url.DocumentViewImpl;
036import org.nuxeo.ecm.platform.url.api.DocumentView;
037import org.nuxeo.ecm.platform.url.api.DocumentViewCodecManager;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * @since 5.5
042 */
043public abstract class AbstractDashBoardItemImpl implements DashBoardItem {
044
045    private static final long serialVersionUID = 1L;
046
047    protected static Log log = LogFactory.getLog(AbstractDashBoardItemImpl.class);
048
049    protected Locale locale;
050
051    protected String getI18nLabel(String label, Locale locale) {
052        if (label == null) {
053            label = "";
054        }
055        if (locale == null) {
056            return label;
057        }
058        return I18NUtils.getMessageString("messages", label, null, locale);
059    }
060
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    public String getI18nDirective() {
074        String directiveKey = getDirective();
075        if (directiveKey == null) {
076            directiveKey = getName();
077        }
078        if (locale == null) {
079            return directiveKey;
080        }
081
082        String directiveLabel = getI18nLabel(directiveKey, locale);
083        if (directiveKey != null && directiveKey.equals(directiveLabel)) {
084            if (needi18n()) {
085                directiveKey = "label.workflow.task." + directiveKey;
086            }
087            String newdirectiveLabel = getI18nLabel(directiveKey, locale);
088            if (!directiveKey.equals(newdirectiveLabel)) {
089                directiveLabel = newdirectiveLabel;
090            }
091        }
092        return directiveLabel;
093    }
094
095    protected boolean isCreatedFromCreateTaskOperation() {
096        return Boolean.parseBoolean(getTask().getVariable("createdFromCreateTaskOperation"));
097    }
098
099    protected boolean needi18n() {
100        if (isCreatedFromCreateTaskOperation()) {
101            return false;
102        }
103        return Boolean.parseBoolean(getTask().getVariable(Task.TaskVariableName.needi18n.name()));
104    }
105
106    public JSONObject asJSON() {
107
108        boolean createdFromCreateTaskOperation = isCreatedFromCreateTaskOperation();
109
110        JSONObject obj = new JSONObject();
111        obj.put("id", getTask().getId());
112        obj.put("docref", getDocument().getRef().toString());
113        obj.put("name", getName());
114        obj.put("taskName", getI18nTaskName());
115        obj.put("directive", getI18nDirective());
116        String comment = getComment();
117        obj.put("comment", comment != null ? comment : "");
118        Date dueDate = getDueDate();
119        obj.put("dueDate", dueDate != null ? DateParser.formatW3CDateTime(dueDate) : "");
120        obj.put("documentTitle", getDocument().getTitle());
121        obj.put("documentLink", getDocumentLink(false));
122        Date startDate = getStartDate();
123        obj.put("startDate", startDate != null ? DateParser.formatW3CDateTime(startDate) : "");
124        boolean expired = false;
125        if (dueDate != null) {
126            expired = dueDate.before(new Date());
127        }
128        obj.put("expired", expired);
129        return obj;
130
131    }
132
133    public String getDocumentLink(boolean includeWorkflowTab) {
134        DocumentModel doc = getDocument();
135        DocumentViewCodecManager documentViewCodecManager = Framework.getService(DocumentViewCodecManager.class);
136        if (documentViewCodecManager != null) {
137            String viewId = getDefaultViewFor(doc);
138            Map<String, String> parameters = new HashMap<String, String>();
139            if (includeWorkflowTab) {
140                parameters.put("tabId", "TAB_CONTENT_JBPM");
141            }
142            DocumentView docView = new DocumentViewImpl(new DocumentLocationImpl(doc), viewId, parameters);
143            return documentViewCodecManager.getUrlFromDocumentView("docpath", docView, false, null);
144        } else {
145            return "";
146        }
147    }
148
149    protected String getDefaultViewFor(DocumentModel doc) {
150        TypeInfo type = doc.getAdapter(TypeInfo.class);
151        if (type == null) {
152            return null;
153        }
154        return type.getDefaultView();
155    }
156
157    public Locale getLocale() {
158        return locale;
159    }
160
161    public void setLocale(Locale locale) {
162        this.locale = locale;
163    }
164
165}