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