001package org.nuxeo.project.sample; 002 003import java.io.Serializable; 004import java.util.ArrayList; 005import java.util.Arrays; 006import java.util.HashSet; 007import java.util.LinkedList; 008import java.util.List; 009import java.util.Random; 010import java.util.Set; 011 012import javax.faces.application.FacesMessage; 013import javax.faces.component.EditableValueHolder; 014import javax.faces.component.UIComponent; 015import javax.faces.context.FacesContext; 016import javax.faces.model.SelectItem; 017 018import org.apache.commons.logging.Log; 019import org.apache.commons.logging.LogFactory; 020import org.jboss.seam.ScopeType; 021import org.jboss.seam.annotations.In; 022import org.jboss.seam.annotations.Name; 023import org.jboss.seam.annotations.Observer; 024import org.jboss.seam.annotations.Scope; 025import org.jboss.seam.annotations.remoting.WebRemote; 026import org.jboss.seam.core.Events; 027import org.jboss.seam.faces.FacesMessages; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.DocumentModelList; 031import org.nuxeo.ecm.directory.Session; 032import org.nuxeo.ecm.directory.api.DirectoryService; 033import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 034import org.nuxeo.ecm.platform.ui.web.api.WebActions; 035import org.nuxeo.ecm.webapp.helpers.EventNames; 036import org.nuxeo.ecm.webapp.helpers.ResourcesAccessor; 037import org.nuxeo.runtime.api.Framework; 038 039@Scope(ScopeType.CONVERSATION) 040@Name("bookManager") 041public class BookManagerBean implements BookManager, Serializable { 042 043 private static final long serialVersionUID = 1L; 044 045 private static final Log log = LogFactory.getLog(BookManagerBean.class); 046 047 @In(create = true) 048 protected transient NavigationContext navigationContext; 049 050 @In(create = true) 051 protected transient WebActions webActions; 052 053 @In(create = true) 054 protected transient CoreSession documentManager; 055 056 @In(create = true) 057 protected transient FacesMessages facesMessages; 058 059 @In(create = true) 060 protected transient ResourcesAccessor resourcesAccessor; 061 062 private String firstName; 063 064 private String lastName; 065 066 private String isbn; 067 068 private List<String> keywords; 069 070 protected List<SelectItem> keywordList; 071 072 private String page; 073 074 private int rating; 075 076 private String filter; 077 078 public String getParentTitle() { 079 DocumentModel doc = navigationContext.getCurrentDocument(); 080 DocumentModel parent = documentManager.getParentDocument(doc.getRef()); 081 return (String) parent.getProperty("dublincore", "title"); 082 } 083 084 public String getFirstName() { 085 if (firstName == null) { 086 firstName = ""; 087 } 088 return firstName; 089 } 090 091 public void setFirstName(String s) { 092 firstName = s; 093 } 094 095 public String getLastName() { 096 if (lastName == null) { 097 lastName = ""; 098 } 099 return lastName; 100 } 101 102 public void setLastName(String s) { 103 lastName = s; 104 } 105 106 public String getIsbn() { 107 if (isbn == null) { 108 return ""; 109 } else { 110 return isbn; 111 } 112 } 113 114 public void setIsbn(String s) { 115 isbn = s; 116 } 117 118 public int getRating() { 119 return rating; 120 } 121 122 public void setRating(int rating) { 123 this.rating = rating; 124 } 125 126 public List<String> getKeywords() { 127 return keywords; 128 } 129 130 public void setKeywords(List<String> keywords) { 131 this.keywords = keywords; 132 } 133 134 public List<SelectItem> getAvailableKeywords() { 135 if (keywordList == null) { 136 computeKeywordValues(); 137 } 138 return keywordList; 139 } 140 141 @Observer(value = { EventNames.DOCUMENT_SELECTION_CHANGED }, create = false) 142 public void resetKeywordValues() { 143 keywordList = null; 144 } 145 146 private void computeKeywordValues() { 147 DirectoryService dirService = Framework.getLocalService(DirectoryService.class); 148 149 try (Session dir = dirService.open("book_keywords")) { 150 DocumentModelList entries = dir.getEntries(); 151 keywordList = new ArrayList<SelectItem>(entries.size()); 152 for (DocumentModel e : entries) { 153 String value = (String) e.getProperty("vocabulary", "id"); 154 String label = (String) e.getProperty("vocabulary", "label"); 155 SelectItem item = new SelectItem(value, label); 156 keywordList.add(item); 157 } 158 } 159 } 160 161 private static Random rand = new Random(); 162 163 protected static String[] FIRSTNAMES = { "Steve", "John", "Raoul", "James" }; 164 165 protected static String[] LASTNAMES = { "Bond", "Einstein", "Tanaka", "Presley" }; 166 167 public void randomFirstName() { 168 firstName = FIRSTNAMES[rand.nextInt(FIRSTNAMES.length)]; 169 } 170 171 public void randomLastName() { 172 lastName = LASTNAMES[rand.nextInt(LASTNAMES.length)]; 173 } 174 175 /* 176 * Validation / change 177 */ 178 179 public void changeData() { 180 if (getFirstName().equals(getLastName())) { 181 facesMessages.add(FacesMessage.SEVERITY_ERROR, "First name and last name must be different"); 182 } 183 184 DocumentModel document = navigationContext.getCurrentDocument(); 185 String title = getFirstName() + " " + getLastName(); 186 document.setProperty("dublincore", "title", title); 187 document.setProperty("book", "rating", Long.valueOf(rating)); 188 document.setProperty("book", "keywords", keywords); 189 190 documentManager.saveDocument(document); 191 documentManager.save(); 192 } 193 194 public void validation(FacesContext context, UIComponent component, Object value) { 195 Integer v = (Integer) value; 196 if ((v.intValue() % 2) != 0) { 197 ((EditableValueHolder) component).setValid(false); 198 FacesMessage message = new FacesMessage(); 199 message.setDetail("The value must be a multiple of 2"); 200 message.setSummary("Not a multiple of 2"); 201 message.setSeverity(FacesMessage.SEVERITY_ERROR); 202 facesMessages.add(component.getId(), message); 203 } 204 } 205 206 /* 207 * Search 208 */ 209 public DocumentModelList getSearchResults() { 210 DocumentModelList result = documentManager.query("SELECT * FROM Book", 10); 211 return result; 212 } 213 214 /* 215 * Ajax 216 */ 217 218 public boolean hasFilter() { 219 return filter != null; 220 } 221 222 public String getFilter() { 223 return filter; 224 } 225 226 public void setFilter(String newfilter) { 227 if (!(filter == null || filter.equals(newfilter))) { 228 // resultsProvidersCache.invalidate(BookResultsProviderFarm.KEYWORD_KEY); 229 } 230 this.filter = newfilter; 231 } 232 233 /* 234 * Seam remoting 235 */ 236 237 /** 238 * @param param some string, that is directly passed from the Javascript code. 239 */ 240 @WebRemote 241 public String something(String param) { 242 return "It worked: " + param; 243 } 244 245 /* 246 * Wizard 247 */ 248 public String toWizardPage(String page) { 249 this.page = page; 250 return "bookwizard"; 251 } 252 253 public String getWizardPage() { 254 return "/incl/bookwizard_page" + page + ".xhtml"; 255 } 256 257 public String validateWizard() { 258 DocumentModel document = navigationContext.getChangeableDocument(); 259 String title = getFirstName() + " " + getLastName(); 260 document.setProperty("dublincore", "title", title); 261 document.setProperty("book", "isbn", isbn); 262 documentManager.saveDocument(document); 263 documentManager.save(); 264 return webActions.setCurrentTabAndNavigate("TAB_VIEW"); 265 } 266 267 /* 268 * Books listing in folder 269 */ 270 public List<BookInfo> getBooksInFolder() { 271 List<BookInfo> list = new LinkedList<BookInfo>(); 272 273 DocumentModel folder = navigationContext.getCurrentDocument(); 274 DocumentModelList children = documentManager.getChildren(folder.getRef(), "Book"); 275 for (DocumentModel doc : children) { 276 String[] keywords = (String[]) doc.getProperty("book", "keywords"); 277 if (keywords == null) { 278 continue; 279 } 280 list.add(new BookInfo(doc, Arrays.asList(keywords))); 281 } 282 return list; 283 } 284 285 public static class BookInfo { 286 287 private DocumentModel doc; 288 289 private List<String> labels; 290 291 public BookInfo(DocumentModel doc, List<String> labels) { 292 this.doc = doc; 293 this.labels = labels; 294 } 295 296 public DocumentModel getDocument() { 297 return doc; 298 } 299 300 public List<String> getLabels() { 301 return labels; 302 } 303 304 } 305 306 /* 307 * Unused 308 */ 309 public String duplicateSiblings() { 310 DocumentModel doc = navigationContext.getCurrentDocument(); 311 DocumentModel folder = documentManager.getParentDocument(doc.getRef()); 312 DocumentModel gp = documentManager.getParentDocument(folder.getRef()); 313 314 // find the other folder names 315 Set<String> names = new HashSet<String>(); 316 for (DocumentModel f : documentManager.getChildren(gp.getRef())) { 317 names.add(f.getName()); 318 } 319 // find a new unique name 320 String newFolderName = folder.getName(); 321 while (names.contains(newFolderName)) { 322 newFolderName += "copy"; 323 } 324 // create the new folder 325 DocumentModel newFolder = documentManager.createDocumentModel(gp.getPathAsString(), newFolderName, 326 folder.getType()); 327 newFolder.setProperty("dublincore", "title", "Nouveau folder"); 328 documentManager.createDocument(newFolder); 329 330 // create the children 331 String newFolderPath = newFolder.getPathAsString(); 332 for (DocumentModel child : documentManager.getChildren(folder.getRef(), "Book")) { 333 DocumentModel newChild = documentManager.createDocumentModel(newFolderPath, child.getName(), "Note"); 334 String title = child.getProperty("dublincore", "title") + " duplicated"; 335 newChild.setProperty("dublincore", "title", title); 336 documentManager.createDocument(newChild); 337 } 338 documentManager.save(); 339 340 Events.instance().raiseEvent(EventNames.DOCUMENT_CHILDREN_CHANGED, gp); 341 return null; 342 } 343 344 public String getContainerPath() { 345 DocumentModel currentDocument = navigationContext.getCurrentDocument(); 346 if (currentDocument.getDocumentType().getName().equals("Book")) 347 return currentDocument.getPath().removeLastSegments(1).toString(); 348 return currentDocument.getPathAsString(); 349 } 350 351}