001/* 002 * (C) Copyright 2012 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 * Thierry Delprat 018 */ 019package org.nuxeo.template.context.extensions; 020 021import java.text.SimpleDateFormat; 022import java.util.ArrayList; 023import java.util.Calendar; 024import java.util.Date; 025import java.util.List; 026import java.util.Locale; 027import java.util.MissingResourceException; 028 029import org.nuxeo.common.utils.i18n.I18NUtils; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.NuxeoPrincipal; 032import org.nuxeo.ecm.directory.Directory; 033import org.nuxeo.ecm.directory.Session; 034import org.nuxeo.ecm.directory.api.DirectoryService; 035import org.nuxeo.ecm.platform.usermanager.UserManager; 036import org.nuxeo.runtime.api.Framework; 037import org.nuxeo.template.api.context.DocumentWrapper; 038 039public class ContextFunctions { 040 041 protected final DocumentModel doc; 042 043 protected final DocumentWrapper nuxeoWrapper; 044 045 public ContextFunctions(DocumentModel doc, DocumentWrapper nuxeoWrapper) { 046 this.doc = doc; 047 this.nuxeoWrapper = nuxeoWrapper; 048 } 049 050 public String getVocabularyTranslatedLabel(String voc_name, String key, String lang) { 051 052 String labelKey = getVocabularyLabel(voc_name, key); 053 if (labelKey == null) { 054 return null; 055 } 056 057 Locale locale = new Locale(lang); 058 if (voc_name.contains("/") && labelKey.contains(" / ")) { 059 String[] parts = labelKey.split(" \\/ "); 060 061 String result = ""; 062 for (int i = 0; i < parts.length; i++) { 063 if (i > 0) { 064 result = result + " / "; 065 } 066 try { 067 result = result + I18NUtils.getMessageString("messages", parts[i], null, locale); 068 } catch (MissingResourceException e) { 069 result = result + parts[i]; 070 } 071 } 072 return result; 073 } else { 074 try { 075 return I18NUtils.getMessageString("messages", labelKey, null, locale); 076 } catch (MissingResourceException e) { 077 return labelKey; 078 } 079 } 080 } 081 082 public String getVocabularyLabel(String voc_name, String key) { 083 084 DirectoryService ds = Framework.getService(DirectoryService.class); 085 if (ds == null) { 086 return key; 087 } 088 089 List<String> vocs = new ArrayList<String>(); 090 List<String> keys = new ArrayList<String>(); 091 if (voc_name.contains("/")) { 092 String[] parts = voc_name.split("\\/"); 093 for (String part : parts) { 094 vocs.add(part); 095 } 096 parts = key.split("\\/"); 097 for (String part : parts) { 098 keys.add(part); 099 } 100 } else { 101 vocs.add(voc_name); 102 keys.add(key); 103 } 104 105 List<String> values = new ArrayList<String>(); 106 107 for (int i = 0; i < vocs.size(); i++) { 108 String voc = vocs.get(i); 109 String keyValue = keys.get(i); 110 if (ds.getDirectoryNames().contains(voc)) { 111 Directory dir = ds.getDirectory(voc); 112 String schema = dir.getSchema(); 113 if ("vocabulary".equals(schema) || "xvocabulary".equals(schema)) { 114 try (Session session = dir.getSession()) { 115 DocumentModel entry = session.getEntry(keyValue); 116 if (entry != null) { 117 values.add((String) entry.getProperty(schema, "label")); 118 } 119 } 120 } 121 } 122 } 123 if (values.size() == 0) { 124 return key; 125 } else if (values.size() == 1) { 126 return values.get(0); 127 } else { 128 String result = ""; 129 for (int i = 0; i < values.size(); i++) { 130 if (i > 0) { 131 result = result + " / "; 132 } 133 result = result + values.get(i); 134 } 135 return result; 136 } 137 } 138 139 public String formatDate(Object calendar) { 140 return formatDate(calendar, "MM/dd/yyyy"); 141 } 142 143 public String formatDateTime(Object calendar) { 144 return formatDate(calendar, "MM/dd/yyyy HH:mm:ss"); 145 } 146 147 public String formatTime(Object calendar) { 148 return formatDate(calendar, "HH:mm:ss"); 149 } 150 151 public String formatDate(Object calendar, String format) { 152 Date dt = null; 153 if (calendar instanceof Calendar) { 154 dt = ((Calendar) calendar).getTime(); 155 } else if (calendar instanceof Date) { 156 dt = (Date) calendar; 157 } 158 159 if (dt == null) { 160 return ""; 161 } 162 SimpleDateFormat dateFormat = new SimpleDateFormat(format); 163 164 return dateFormat.format(calendar); 165 } 166 167 public NuxeoPrincipal getNuxeoPrincipal(String username) { 168 UserManager userManager = Framework.getService(UserManager.class); 169 if (userManager == null) { 170 return null; 171 } 172 return userManager.getPrincipal(username); 173 } 174}