1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.ecm.webui.component.explorer.upload;
18
19 import java.io.BufferedInputStream;
20 import java.io.ByteArrayInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.InputStream;
23 import java.security.AccessControlException;
24 import java.util.ArrayList;
25 import java.util.GregorianCalendar;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.MissingResourceException;
30
31 import javax.jcr.AccessDeniedException;
32 import javax.jcr.ItemExistsException;
33 import javax.jcr.ItemNotFoundException;
34 import javax.jcr.NamespaceRegistry;
35 import javax.jcr.Node;
36 import javax.jcr.RepositoryException;
37 import javax.jcr.Session;
38 import javax.jcr.lock.LockException;
39 import javax.jcr.nodetype.ConstraintViolationException;
40 import javax.portlet.PortletPreferences;
41
42 import org.apache.commons.lang.StringUtils;
43 import org.exoplatform.ecm.utils.text.Text;
44 import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
45 import org.exoplatform.ecm.webui.form.validator.IllegalDMSCharValidator;
46 import org.exoplatform.ecm.webui.selector.UISelectable;
47 import org.exoplatform.ecm.webui.tree.selectone.UIOneTaxonomySelector;
48 import org.exoplatform.ecm.webui.utils.JCRExceptionManager;
49 import org.exoplatform.ecm.utils.lock.LockUtil;
50 import org.exoplatform.ecm.webui.utils.Utils;
51 import org.exoplatform.services.cms.BasePath;
52 import org.exoplatform.services.cms.CmsService;
53 import org.exoplatform.services.cms.JcrInputProperty;
54 import org.exoplatform.services.cms.documents.DocumentTypeService;
55 import org.exoplatform.services.cms.impl.DMSConfiguration;
56 import org.exoplatform.services.cms.impl.DMSRepositoryConfiguration;
57 import org.exoplatform.services.cms.link.LinkManager;
58 import org.exoplatform.services.cms.mimetype.DMSMimeTypeResolver;
59 import org.exoplatform.services.cms.taxonomy.TaxonomyService;
60 import org.exoplatform.services.jcr.RepositoryService;
61 import org.exoplatform.services.jcr.access.PermissionType;
62 import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
63 import org.exoplatform.services.jcr.impl.core.NodeImpl;
64 import org.exoplatform.services.listener.ListenerService;
65 import org.exoplatform.services.log.ExoLogger;
66 import org.exoplatform.services.log.Log;
67 import org.exoplatform.services.wcm.core.NodeLocation;
68 import org.exoplatform.services.wcm.core.NodetypeConstant;
69 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
70 import org.exoplatform.upload.UploadResource;
71 import org.exoplatform.upload.UploadService;
72 import org.exoplatform.web.application.ApplicationMessage;
73 import org.exoplatform.webui.application.WebuiRequestContext;
74 import org.exoplatform.webui.application.portlet.PortletRequestContext;
75 import org.exoplatform.webui.config.annotation.ComponentConfig;
76 import org.exoplatform.webui.config.annotation.ComponentConfigs;
77 import org.exoplatform.webui.config.annotation.EventConfig;
78 import org.exoplatform.webui.core.UIApplication;
79 import org.exoplatform.webui.core.UIComponent;
80 import org.exoplatform.webui.core.UIPopupComponent;
81 import org.exoplatform.webui.core.UIPopupWindow;
82 import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
83 import org.exoplatform.webui.event.Event;
84 import org.exoplatform.webui.event.Event.Phase;
85 import org.exoplatform.webui.event.EventListener;
86 import org.exoplatform.webui.exception.MessageException;
87 import org.exoplatform.webui.form.UIForm;
88 import org.exoplatform.webui.form.UIFormMultiValueInputSet;
89 import org.exoplatform.webui.form.UIFormStringInput;
90 import org.exoplatform.webui.form.input.UIUploadInput;
91 import org.exoplatform.webui.form.validator.MandatoryValidator;
92
93
94
95
96
97
98
99
100
101 @ComponentConfigs(
102 {
103 @ComponentConfig(
104 lifecycle = UIFormLifecycle.class,
105 template = "app:/groovy/webui/component/explorer/upload/UIUploadForm.gtmpl",
106 events = {
107 @EventConfig(listeners = UIUploadForm.SaveActionListener.class),
108 @EventConfig(listeners = UIUploadForm.CancelActionListener.class, phase = Phase.DECODE),
109 @EventConfig(listeners = UIUploadForm.AddUploadActionListener.class, phase = Phase.DECODE),
110 @EventConfig(listeners = UIUploadForm.RemoveUploadActionListener.class, phase = Phase.DECODE)
111 }
112 ),
113 @ComponentConfig(
114 type = UIFormMultiValueInputSet.class,
115 id="UploadMultipleInputset",
116 events = {
117 @EventConfig(listeners = UIUploadForm.RemoveActionListener.class, phase = Phase.DECODE),
118 @EventConfig(listeners = UIUploadForm.AddActionListener.class, phase = Phase.DECODE)
119 }
120 )
121 }
122 )
123
124 public class UIUploadForm extends UIForm implements UIPopupComponent, UISelectable {
125
126
127
128
129 private static final Log LOG = ExoLogger.getLogger(UIUploadForm.class.getName());
130
131 final static public String FIELD_NAME = "name" ;
132 final static public String FIELD_UPLOAD = "upload" ;
133 final static public String JCRCONTENT = "jcr:content";
134 final static public String FIELD_TAXONOMY = "fieldTaxonomy";
135 final static public String FIELD_LISTTAXONOMY = "fieldListTaxonomy";
136 final static public String POPUP_TAXONOMY = "UIPopupTaxonomy";
137 final static public String ACCESSIBLE_MEDIA = "accessibleMedia";
138
139 private boolean isMultiLanguage_;
140 private List<String> listTaxonomy = new ArrayList<String>();
141 private List<String> listTaxonomyName = new ArrayList<String>();
142
143 private int numberUploadFile = 1;
144 private HashMap<String, List<String>> mapTaxonomies = new HashMap<String, List<String>>();
145 private List<NodeLocation> listUploadedNodes = new ArrayList<NodeLocation>();
146 private boolean taxonomyMandatory = false;
147
148 private DocumentTypeService docService;
149
150 public UIUploadForm() throws Exception {
151 setMultiPart(true) ;
152 addUIFormInput(new UIFormStringInput(FIELD_NAME, FIELD_NAME, null).
153 addValidator(IllegalDMSCharValidator.class));
154 PortletRequestContext pcontext = (PortletRequestContext)WebuiRequestContext.getCurrentInstance();
155 PortletPreferences portletPref = pcontext.getRequest().getPreferences();
156 String limitPref = portletPref.getValue(Utils.UPLOAD_SIZE_LIMIT_MB, "");
157 UIUploadInput uiInput = null;
158 if (limitPref != null) {
159 try {
160 uiInput = new UIUploadInput(FIELD_UPLOAD, FIELD_UPLOAD, 1, Integer.parseInt(limitPref.trim()));
161 } catch (NumberFormatException e) {
162 uiInput = new UIUploadInput(FIELD_UPLOAD, FIELD_UPLOAD);
163 }
164 } else {
165 uiInput = new UIUploadInput(FIELD_UPLOAD, FIELD_UPLOAD);
166 }
167 addUIFormInput(uiInput);
168 docService = WCMCoreUtils.getService(DocumentTypeService.class);
169 }
170
171 public int getNumberUploadFile() {
172 return numberUploadFile;
173 }
174
175 public void setNumberUploadFile(int numberUpload) {
176 numberUploadFile = numberUpload;
177 }
178
179 public HashMap<String, List<String>> getMapTaxonomies() {
180 return mapTaxonomies;
181 }
182
183 public void setMapTaxonomies(HashMap<String, List<String>> mapTaxonomiesAvaiable) {
184 mapTaxonomies = mapTaxonomiesAvaiable;
185 }
186
187 public List<String> getListTaxonomy() {
188 return listTaxonomy;
189 }
190
191 public List<String> getlistTaxonomyName() {
192 return listTaxonomyName;
193 }
194
195 public void setListTaxonomy(List<String> listTaxonomyNew) {
196 listTaxonomy = listTaxonomyNew;
197 }
198
199 public void setListTaxonomyName(List<String> listTaxonomyNameNew) {
200 listTaxonomyName = listTaxonomyNameNew;
201 }
202
203 public boolean getTaxonomyMandatory() {
204 return taxonomyMandatory;
205 }
206
207 public void setTaxonomyMandatory(boolean taxoMandatory) {
208 taxonomyMandatory = taxoMandatory;
209 }
210
211 public String getPathTaxonomy() throws Exception {
212 UIJCRExplorer uiExplorer = getAncestorOfType(UIJCRExplorer.class);
213 DMSConfiguration dmsConfig = getApplicationComponent(DMSConfiguration.class);
214 DMSRepositoryConfiguration dmsRepoConfig = dmsConfig.getConfig();
215 String workspaceName = dmsRepoConfig.getSystemWorkspace();
216 NodeHierarchyCreator nodeHierarchyCreator = getApplicationComponent(NodeHierarchyCreator.class);
217 Session session = uiExplorer.getSessionByWorkspace(workspaceName);
218 return ((Node)session.getItem(nodeHierarchyCreator.getJcrPath(BasePath.TAXONOMIES_TREE_STORAGE_PATH))).getPath();
219 }
220
221 public void initFieldInput() throws Exception {
222 PortletRequestContext pcontext = (PortletRequestContext)WebuiRequestContext.getCurrentInstance();
223 PortletPreferences portletPref = pcontext.getRequest().getPreferences();
224 String categoryMandatoryWhenFileUpload = portletPref.getValue(Utils.CATEGORY_MANDATORY, "").trim();
225 UIFormMultiValueInputSet uiFormMultiValue = createUIComponent(UIFormMultiValueInputSet.class,
226 "UploadMultipleInputset",
227 null);
228 uiFormMultiValue.setId(FIELD_LISTTAXONOMY);
229 uiFormMultiValue.setName(FIELD_LISTTAXONOMY);
230 uiFormMultiValue.setType(UIFormStringInput.class);
231 uiFormMultiValue.setEditable(false);
232 if (categoryMandatoryWhenFileUpload.equalsIgnoreCase("true")) {
233 uiFormMultiValue.addValidator(MandatoryValidator.class);
234 setTaxonomyMandatory(true);
235 } else {
236 setTaxonomyMandatory(false);
237 }
238 uiFormMultiValue.setValue(listTaxonomyName);
239 addUIFormInput(uiFormMultiValue);
240 }
241
242 public String[] getActions() {
243 return new String[] {"Save", "Cancel"};
244 }
245
246 public void setIsMultiLanguage(boolean isMultiLanguage, String language) {
247 isMultiLanguage_ = isMultiLanguage ;
248 }
249
250 public void resetComponent() {
251 removeChildById(FIELD_UPLOAD);
252 addUIFormInput(new UIUploadInput(FIELD_UPLOAD, FIELD_UPLOAD));
253 }
254
255 public boolean isMultiLanguage() { return isMultiLanguage_ ; }
256
257 public void activate() {}
258 public void deActivate() {}
259
260 public void doSelect(String selectField, Object value) throws Exception {
261 String valueTaxonomy = String.valueOf(value).trim();
262 List<String> indexMapTaxonomy = new ArrayList<String>();
263 if (mapTaxonomies.containsKey(selectField)){
264 indexMapTaxonomy = mapTaxonomies.get(selectField);
265 mapTaxonomies.remove(selectField);
266 }
267 if (!indexMapTaxonomy.contains(valueTaxonomy)) indexMapTaxonomy.add(valueTaxonomy);
268 mapTaxonomies.put(selectField, indexMapTaxonomy);
269
270 updateAdvanceTaxonomy(selectField);
271 UIUploadManager uiUploadManager = getParent();
272 uiUploadManager.removeChildById(POPUP_TAXONOMY);
273 }
274
275 public List<String> getListSameNames(Event<UIUploadForm> event) throws Exception {
276 List<String> sameNameList = new ArrayList<String>();
277 Node selectedNode = getAncestorOfType(UIJCRExplorer.class).getCurrentNode();
278 int index = 0;
279 String name = null;
280 for (UIComponent uiComp : getChildren()) {
281 if(uiComp instanceof UIUploadInput) {
282 String[] arrayId = uiComp.getId().split(FIELD_UPLOAD);
283 if ((arrayId.length > 0) && (arrayId[0].length() > 0)) index = new Integer(arrayId[0]).intValue();
284 UIUploadInput uiUploadInput;
285 if (index == 0){
286 uiUploadInput = (UIUploadInput)getUIInput(FIELD_UPLOAD);
287 } else {
288 uiUploadInput = (UIUploadInput)getUIInput(index + FIELD_UPLOAD);
289 }
290 String uploadId = uiUploadInput.getUploadIds()[0];
291 if (uiUploadInput.getUploadResource(uploadId) == null) return sameNameList;
292 String fileName = uiUploadInput.getUploadResource(uploadId).getFileName();
293 if (index == 0){
294 name = getUIStringInput(FIELD_NAME).getValue();
295 } else {
296 name = getUIStringInput(index + FIELD_NAME).getValue();
297 }
298 if(name == null) {
299 name = fileName;
300 } else {
301 name = name.trim();
302 }
303 name = Text.escapeIllegalJcrChars(name);
304 if (!passNameValidation(name)) {
305 return new ArrayList<String>();
306 }
307 if(selectedNode.hasNode(name)) sameNameList.add(name);
308 }
309 }
310 return sameNameList;
311 }
312
313 @SuppressWarnings("rawtypes")
314 public void doUpload(Event event, boolean isKeepFile) throws Exception {
315 UIApplication uiApp = getAncestorOfType(UIApplication.class) ;
316 UIJCRExplorer uiExplorer = getAncestorOfType(UIJCRExplorer.class) ;
317
318 UIUploadManager uiManager = getParent();
319 UIUploadContainer uiUploadContainer = uiManager.getChild(UIUploadContainer.class);
320 UploadService uploadService = getApplicationComponent(UploadService.class);
321 UIUploadContent uiUploadContent = uiManager.findFirstComponentOfType(UIUploadContent.class);
322 List<String[]> listArrValues = new ArrayList<String[]>();
323 CmsService cmsService = getApplicationComponent(CmsService.class) ;
324 List<UIComponent> listFormChildren = getChildren();
325 int index = 0;
326 InputStream inputStream;
327 String name = null;
328 TaxonomyService taxonomyService = getApplicationComponent(TaxonomyService.class);
329 if(uiExplorer.getCurrentNode().isLocked()) {
330 String lockToken = LockUtil.getLockToken(uiExplorer.getCurrentNode());
331 if(lockToken != null) uiExplorer.getSession().addLockToken(lockToken);
332 }
333 PortletRequestContext pcontext = (PortletRequestContext)WebuiRequestContext.getCurrentInstance();
334 PortletPreferences portletPref = pcontext.getRequest().getPreferences();
335 String categoryMandatoryWhenFileUpload = portletPref.getValue(Utils.CATEGORY_MANDATORY, "").trim();
336 DMSMimeTypeResolver mimeTypeSolver = DMSMimeTypeResolver.getInstance();
337 Node selectedNode = uiExplorer.getCurrentNode();
338 if (categoryMandatoryWhenFileUpload.equalsIgnoreCase("true") &&
339 (getMapTaxonomies().size() == 0) && !uiExplorer.getCurrentNode().hasNode(JCRCONTENT)) {
340 uiApp.addMessage(new ApplicationMessage("UIUploadForm.msg.taxonomyPath-error", null,
341 ApplicationMessage.WARNING)) ;
342
343 return ;
344 }
345 String pers = PermissionType.ADD_NODE + "," + PermissionType.SET_PROPERTY ;
346 selectedNode.getSession().checkPermission(selectedNode.getPath(), pers);
347 try {
348 int indexValidate = 0;
349 for (UIComponent uiCompValidate : listFormChildren) {
350 if(uiCompValidate instanceof UIUploadInput) {
351 String[] arrayIdValidate = uiCompValidate.getId().split(FIELD_UPLOAD);
352 if ((arrayIdValidate.length > 0) && (arrayIdValidate[0].length() > 0))
353 indexValidate = new Integer(arrayIdValidate[0]).intValue();
354 UIUploadInput uiUploadInput;
355 if (indexValidate == 0){
356 uiUploadInput = (UIUploadInput) getUIInput(FIELD_UPLOAD);
357 } else {
358 uiUploadInput = (UIUploadInput) getUIInput(indexValidate + FIELD_UPLOAD);
359 }
360 String uploadId = uiUploadInput.getUploadIds()[0];
361 if(uiUploadInput.getUploadResource(uploadId) == null) {
362 uiApp.addMessage(new ApplicationMessage("UIUploadForm.msg.fileName-error", null,
363 ApplicationMessage.WARNING)) ;
364
365 return ;
366 }
367 String fileName = uiUploadInput.getUploadResource(uploadId).getFileName();
368 if(fileName == null || fileName.length() == 0) {
369 uiApp.addMessage(new ApplicationMessage("UIUploadForm.msg.fileName-error", null,
370 ApplicationMessage.WARNING)) ;
371
372 return;
373 }
374 }
375 }
376 for (UIComponent uiComp : listFormChildren) {
377 if(uiComp instanceof UIUploadInput) {
378 String[] arrayId = uiComp.getId().split(FIELD_UPLOAD);
379 if ((arrayId.length > 0) && (arrayId[0].length() > 0)) index = new Integer(arrayId[0]).intValue();
380 UIUploadInput uiUploadInput;
381 if (index == 0){
382 uiUploadInput = (UIUploadInput) getUIInput(FIELD_UPLOAD);
383 } else {
384 uiUploadInput = (UIUploadInput) getUIInput(index + FIELD_UPLOAD);
385 }
386 String uploadId = uiUploadInput.getUploadIds()[0];
387 if(uiUploadInput.getUploadResource(uploadId) == null) {
388 if ((listUploadedNodes != null) && (listUploadedNodes.size() > 0)) {
389 for (Object uploadedNode : NodeLocation.getNodeListByLocationList(listUploadedNodes)) {
390 ((Node)uploadedNode).remove();
391 }
392 uiExplorer.getCurrentNode().save();
393 listUploadedNodes.clear();
394 }
395 uiApp.addMessage(new ApplicationMessage("UIUploadForm.msg.fileName-error", null,
396 ApplicationMessage.WARNING)) ;
397
398 return ;
399 }
400
401 String fileName = uiUploadInput.getUploadResource(uploadId).getFileName();
402
403 if(fileName == null || fileName.length() == 0) {
404 if ((listUploadedNodes != null) && (listUploadedNodes.size() > 0)) {
405 for (Object uploadedNode : NodeLocation.getNodeListByLocationList(listUploadedNodes)) {
406 ((Node)uploadedNode).remove();
407 }
408 uiExplorer.getCurrentNode().save();
409 listUploadedNodes.clear();
410 }
411 uiApp.addMessage(new ApplicationMessage("UIUploadForm.msg.fileName-error", null,
412 ApplicationMessage.WARNING)) ;
413
414 return;
415 }
416 try {
417 inputStream = new BufferedInputStream(uiUploadInput.getUploadDataAsStream(uploadId));
418 } catch (FileNotFoundException e) {
419 inputStream = new BufferedInputStream(new ByteArrayInputStream(new byte[] {}));
420 }
421 if (index == 0){
422 name = getUIStringInput(FIELD_NAME).getValue();
423 } else {
424 name = getUIStringInput(index + FIELD_NAME).getValue();
425 }
426
427 if(name == null) name = fileName;
428 else name = name.trim();
429
430 if (!passNameValidation(name)) {
431 uiApp.addMessage(new ApplicationMessage("UIUploadForm.msg.fileName-invalid-with-name",
432 new Object[] {name}, ApplicationMessage.WARNING));
433
434 return;
435 }
436
437 name = Text.escapeIllegalJcrChars(name);
438
439
440 String mimeType = mimeTypeSolver.getMimeType(fileName);
441 String ext = "." + fileName.substring(fileName.lastIndexOf(".") + 1);
442 if (name.lastIndexOf(ext) < 0 && !mimeTypeSolver.getMimeType(name).equals(mimeType)) {
443 StringBuffer sb = new StringBuffer();
444 sb.append(name).append(ext);
445 name = sb.toString();
446 }
447
448 List<String> listTaxonomyNameNew = new ArrayList<String>();
449 if (index == 0) listTaxonomyNameNew = mapTaxonomies.get(FIELD_LISTTAXONOMY);
450 else listTaxonomyNameNew = mapTaxonomies.get(index + FIELD_LISTTAXONOMY);
451 String taxonomyTree = null;
452 String taxonomyPath = null;
453 if (listTaxonomyNameNew != null) {
454 for(String categoryPath : listTaxonomyNameNew) {
455 try {
456 if (categoryPath.startsWith("/")) categoryPath = categoryPath.substring(1);
457 if(categoryPath.indexOf("/")>0)
458 {
459 taxonomyTree = categoryPath.substring(0, categoryPath.indexOf("/"));
460 taxonomyPath = categoryPath.substring(categoryPath.indexOf("/") + 1);
461 taxonomyService.getTaxonomyTree(taxonomyTree).hasNode(taxonomyPath);
462 }
463 else
464 {
465 taxonomyTree = categoryPath;
466 taxonomyPath = "";
467 }
468 } catch (ItemNotFoundException e) {
469 uiApp.addMessage(new ApplicationMessage("UISelectedCategoriesGrid.msg.non-categories", null,
470 ApplicationMessage.WARNING)) ;
471
472 return;
473 } catch (RepositoryException re) {
474 uiApp.addMessage(new ApplicationMessage("UISelectedCategoriesGrid.msg.non-categories", null,
475 ApplicationMessage.WARNING)) ;
476
477 return;
478 } catch(Exception e) {
479 if (LOG.isErrorEnabled()) {
480 LOG.error("An unexpected error occurs", e);
481 }
482 uiApp.addMessage(new ApplicationMessage("UISelectedCategoriesGrid.msg.non-categories", null,
483 ApplicationMessage.WARNING)) ;
484
485 return;
486 }
487 }
488 }
489 boolean isExist = selectedNode.hasNode(name) ;
490 String newNodeUUID = null;
491
492 if(selectedNode.getPrimaryNodeType().isNodeType(Utils.NT_FILE)) {
493 if(!selectedNode.isCheckedOut()) selectedNode.checkout() ;
494 Node contentNode = selectedNode.getNode(Utils.JCR_CONTENT);
495 if(contentNode.getProperty(Utils.JCR_MIMETYPE).getString().equals(mimeType)) {
496 contentNode.setProperty(Utils.JCR_DATA, inputStream);
497 contentNode.setProperty(Utils.JCR_LASTMODIFIED, new GregorianCalendar());
498 selectedNode.save() ;
499 uiManager.setRendered(false);
500 uiExplorer.updateAjax(event);
501 return;
502 }
503 }
504 if(!isExist || isKeepFile) {
505 String nodeType = contains(docService.getMimeTypes(ACCESSIBLE_MEDIA), mimeType) ?
506 NodetypeConstant.EXO_ACCESSIBLE_MEDIA : Utils.NT_FILE;
507 newNodeUUID = cmsService.storeNodeByUUID(nodeType, selectedNode,
508 getInputProperties(name, inputStream, mimeType), true) ;
509 selectedNode.save();
510 selectedNode.getSession().save();
511 if ((listTaxonomyNameNew != null) && (listTaxonomyNameNew.size() > 0)) {
512 Node newNode = null;
513 try {
514 newNode = selectedNode.getSession().getNodeByUUID(newNodeUUID);
515 } catch(ItemNotFoundException e) {
516 newNode = Utils.findNodeByUUID(newNodeUUID);
517 }
518 if (newNode != null) {
519 for (String categoryPath : listTaxonomyNameNew) {
520 try {
521 if (categoryPath.startsWith("/")) categoryPath = categoryPath.substring(1);
522 if(categoryPath.indexOf("/")>0)
523 {
524 taxonomyTree = categoryPath.substring(0, categoryPath.indexOf("/"));
525 taxonomyPath = categoryPath.substring(categoryPath.indexOf("/") + 1);
526 }
527 else
528 {
529 taxonomyTree = categoryPath;
530 taxonomyPath = "";
531 }
532 taxonomyService.addCategory(newNode, taxonomyTree, taxonomyPath);
533 } catch (ItemExistsException e) {
534 uiApp.addMessage(new ApplicationMessage("UIUploadForm.msg.ItemExistsException",
535 null, ApplicationMessage.WARNING));
536 return;
537 } catch (RepositoryException e) {
538 if (LOG.isErrorEnabled()) {
539 LOG.error("Unexpected error", e);
540 }
541 JCRExceptionManager.process(uiApp, e);
542 return;
543 }
544 }
545 }
546 }
547 } else {
548 Node node = selectedNode.getNode(name) ;
549 if (isTaxonomyChildNode(node)) {
550 LinkManager linkManager = getApplicationComponent(LinkManager.class);
551 node = linkManager.getTarget(node);
552 }
553 if(!node.getPrimaryNodeType().isNodeType(Utils.NT_FILE)) {
554 Object[] args = { name } ;
555 uiApp.addMessage(new ApplicationMessage("UIUploadForm.msg.name-is-exist", args,
556 ApplicationMessage.WARNING)) ;
557
558 return ;
559 }
560 String nodetypes = System.getProperty("wcm.nodetypes.ignoreversion");
561 if(nodetypes == null || nodetypes.length() == 0)
562 nodetypes = "exo:webContent";
563 if(Utils.isMakeVersionable(node, nodetypes.split(","))) {
564 if(!node.isNodeType(Utils.MIX_VERSIONABLE) && node.canAddMixin(Utils.MIX_VERSIONABLE)) {
565 node.addMixin(Utils.MIX_VERSIONABLE);
566 }
567 }
568 Node contentNode = node.getNode(Utils.JCR_CONTENT);
569 if(!node.isCheckedOut()) node.checkout() ;
570 contentNode.setProperty(Utils.JCR_DATA, inputStream);
571 contentNode.setProperty(Utils.JCR_MIMETYPE, mimeType);
572 contentNode.setProperty(Utils.JCR_LASTMODIFIED, new GregorianCalendar());
573 if (node.isNodeType("exo:datetime")) {
574 node.setProperty("exo:dateModified",new GregorianCalendar()) ;
575 }
576 node.save();
577 ListenerService listenerService = getApplicationComponent(ListenerService.class);
578 listenerService.broadcast(CmsService.POST_EDIT_CONTENT_EVENT, this, node);
579 if (listTaxonomyNameNew != null) {
580 for (String categoryPath : listTaxonomyNameNew) {
581 try {
582 if (categoryPath.startsWith("/")) categoryPath = categoryPath.substring(1);
583 if(categoryPath.indexOf("/")>0)
584 {
585 taxonomyTree = categoryPath.substring(0, categoryPath.indexOf("/"));
586 taxonomyPath = categoryPath.substring(categoryPath.indexOf("/") + 1);
587 }
588 else
589 {
590 taxonomyTree = categoryPath;
591 taxonomyPath = "";
592 }
593 taxonomyService.addCategory(node, taxonomyTree, taxonomyPath);
594 } catch (ItemExistsException e) {
595 uiApp.addMessage(new ApplicationMessage("UIUploadForm.msg.ItemExistsException",
596 null, ApplicationMessage.WARNING));
597 return;
598 } catch (RepositoryException e) {
599 if (LOG.isErrorEnabled()) {
600 LOG.error("Unexpected error", e);
601 }
602 JCRExceptionManager.process(uiApp, e);
603 return;
604 }
605 }
606 }
607 }
608 uiExplorer.getSession().save() ;
609
610 Node uploadedNode = null;
611 if(isMultiLanguage_) {
612 uiUploadContainer.setUploadedNode(selectedNode);
613 uploadedNode = selectedNode;
614 } else {
615 Node newNode = null ;
616 if(!isExist) {
617 try {
618 newNode = selectedNode.getSession().getNodeByUUID(newNodeUUID);
619 } catch(ItemNotFoundException e) {
620 newNode = Utils.findNodeByUUID(newNodeUUID);
621 }
622 } else {
623 newNode = selectedNode.getNode(name) ;
624 }
625 if(newNode != null) {
626 uiUploadContainer.setUploadedNode(newNode);
627 uploadedNode = newNode;
628 }
629 }
630 if(mimeType.indexOf(Utils.FLASH_MIMETYPE) >= 0 && uploadedNode.canAddMixin(Utils.EXO_RISIZEABLE)) {
631 uploadedNode.addMixin(Utils.EXO_RISIZEABLE);
632 uploadedNode.save();
633 }
634
635 double size = 0;
636 if (uploadedNode.hasNode(Utils.JCR_CONTENT)) {
637 Node contentNode = uploadedNode.getNode(Utils.JCR_CONTENT);
638 if (contentNode.hasProperty(Utils.JCR_DATA)) {
639 size = contentNode.getProperty(Utils.JCR_DATA).getLength();
640 }
641 } else {
642 size = uploadService.getUploadResource(uiUploadInput.getUploadIds()[0]).getEstimatedSize();
643 }
644 String fileSize = Utils.calculateFileSize(size);
645 String iconUpload = Utils.getNodeTypeIcon(uploadedNode, "16x16Icon").replaceAll("nt_file16x16Icon ", "");
646 String[] arrValues = {iconUpload, Text.unescapeIllegalJcrChars(fileName),
647 Text.unescapeIllegalJcrChars(name), fileSize, mimeType, uploadedNode.getPath()};
648 listUploadedNodes.add(NodeLocation.getNodeLocationByNode(uploadedNode));
649 listArrValues.add(arrValues);
650 inputStream.close();
651 }
652 }
653 uiUploadContent.setListUploadValues(listArrValues);
654 uiManager.setRenderedChild(UIUploadContainer.class);
655 uiExplorer.setIsHidePopup(true);
656 uiExplorer.updateAjax(event);
657 event.getRequestContext().addUIComponentToUpdateByAjax(uiManager);
658 } catch(ConstraintViolationException con) {
659 Object[] args = {name, } ;
660 throw new MessageException(new ApplicationMessage("UIUploadForm.msg.contraint-violation",
661 args, ApplicationMessage.WARNING)) ;
662 } catch(LockException lock) {
663 throw new MessageException(new ApplicationMessage("UIUploadForm.msg.lock-exception",
664 null, ApplicationMessage.WARNING)) ;
665 } catch(AccessDeniedException ade) {
666 throw new MessageException(new ApplicationMessage("UIActionBar.msg.access-add-denied",
667 null, ApplicationMessage.WARNING));
668 } catch(AccessControlException ace) {
669 throw new MessageException(new ApplicationMessage("UIActionBar.msg.access-add-denied",
670 null, ApplicationMessage.WARNING));
671 } catch (ItemExistsException iee) {
672 uiApp.addMessage(new ApplicationMessage("UIActionBar.msg.item-existed", null, ApplicationMessage.WARNING));
673 } catch(Exception e) {
674 if (LOG.isErrorEnabled()) {
675 LOG.error("An unexpected error occurs", e);
676 }
677 JCRExceptionManager.process(uiApp, e);
678 return ;
679 }
680 }
681
682
683
684
685
686
687
688 private boolean isTaxonomyChildNode(Node node) throws RepositoryException {
689 Node parrentNode = node.getParent();
690 while (!((NodeImpl) parrentNode).isRoot()) {
691 if (parrentNode.isNodeType(Utils.EXO_TAXONOMY)) {
692 return true;
693 }
694 parrentNode = parrentNode.getParent();
695 }
696 return false;
697 }
698
699 private Map<String, JcrInputProperty> getInputProperties(String name, InputStream inputStream, String mimeType) {
700 Map<String,JcrInputProperty> inputProperties = new HashMap<String,JcrInputProperty>() ;
701 JcrInputProperty nodeInput = new JcrInputProperty() ;
702 nodeInput.setJcrPath("/node") ;
703 nodeInput.setValue(name) ;
704 nodeInput.setMixintype("mix:i18n,mix:votable,mix:commentable") ;
705 nodeInput.setType(JcrInputProperty.NODE) ;
706 inputProperties.put("/node",nodeInput) ;
707
708 JcrInputProperty jcrContent = new JcrInputProperty() ;
709 jcrContent.setJcrPath("/node/jcr:content") ;
710 jcrContent.setValue("") ;
711 jcrContent.setMixintype("dc:elementSet") ;
712 jcrContent.setNodetype(Utils.NT_RESOURCE) ;
713 jcrContent.setType(JcrInputProperty.NODE) ;
714 inputProperties.put("/node/jcr:content",jcrContent) ;
715
716 JcrInputProperty jcrData = new JcrInputProperty() ;
717 jcrData.setJcrPath("/node/jcr:content/jcr:data") ;
718 jcrData.setValue(inputStream) ;
719 inputProperties.put("/node/jcr:content/jcr:data",jcrData) ;
720
721 JcrInputProperty jcrMimeType = new JcrInputProperty() ;
722 jcrMimeType.setJcrPath("/node/jcr:content/jcr:mimeType") ;
723 jcrMimeType.setValue(mimeType) ;
724 inputProperties.put("/node/jcr:content/jcr:mimeType",jcrMimeType) ;
725
726 JcrInputProperty jcrLastModified = new JcrInputProperty() ;
727 jcrLastModified.setJcrPath("/node/jcr:content/jcr:lastModified") ;
728 jcrLastModified.setValue(new GregorianCalendar()) ;
729 inputProperties.put("/node/jcr:content/jcr:lastModified",jcrLastModified) ;
730
731 JcrInputProperty jcrEncoding = new JcrInputProperty() ;
732 jcrEncoding.setJcrPath("/node/jcr:content/jcr:encoding") ;
733 jcrEncoding.setValue("UTF-8") ;
734 inputProperties.put("/node/jcr:content/jcr:encoding",jcrEncoding) ;
735 return inputProperties;
736 }
737
738 private void updateAdvanceTaxonomy(String selectField) throws Exception {
739 List<UIComponent> listChildren = getChildren();
740 for (UIComponent uiComp : listChildren) {
741 if (uiComp.getId().equals(selectField)) {
742 UIFormMultiValueInputSet uiFormMultiValueInputSet = getChildById(selectField);
743 if (mapTaxonomies.containsKey(selectField))
744 uiFormMultiValueInputSet.setValue(getTaxonomyLabel(mapTaxonomies.get(selectField)));
745 }
746 }
747 }
748
749 private List<String> getTaxonomyLabel(List<String> taxonomyPaths) {
750 List<String> taxonomyLabels = new ArrayList<String>();
751 String[] taxonomyPathSplit = null;
752 StringBuilder buildlabel;
753 StringBuilder buildPathlabel;
754 for (String taxonomyPath : taxonomyPaths) {
755 if (taxonomyPath.startsWith("/"))
756 taxonomyPath = taxonomyPath.substring(1);
757 taxonomyPathSplit = taxonomyPath.split("/");
758 buildlabel = new StringBuilder();
759 buildPathlabel = new StringBuilder();
760 for (int i = 0; i < taxonomyPathSplit.length; i++) {
761 buildlabel = new StringBuilder("eXoTaxonomies");
762 try {
763 for (int j = 0; j <= i; j++) {
764 buildlabel.append(".").append(taxonomyPathSplit[j]);
765 }
766 buildPathlabel.append(Utils.getResourceBundle(buildlabel.append(".label").toString())).append("/");
767 } catch (MissingResourceException me) {
768 buildPathlabel.append(taxonomyPathSplit[i]).append("/");
769 }
770
771 }
772
773 taxonomyLabels.add(buildPathlabel.substring(0, buildPathlabel.length() - 1));
774 }
775 return taxonomyLabels;
776 }
777
778 private boolean passNameValidation(String name) throws Exception {
779 if (name == null || name.contains("[") || name.contains("]") ||
780 name.contains("\"") || name.contains("/"))
781 return false;
782
783 int count = 0;
784 for (char c : name.toCharArray()) {
785 if (c == ':') count ++;
786 if (count > 1) return false;
787 }
788
789 if (count == 1) {
790 if (name.split(":").length < 2) return false;
791 String namespace = name.split(":")[0];
792 NamespaceRegistry namespaceRegistry = getApplicationComponent(RepositoryService.class)
793 .getRepository(getAncestorOfType(UIJCRExplorer.class).getRepositoryName()).getNamespaceRegistry() ;
794 String[] prefixs = namespaceRegistry.getPrefixes();
795 for (String prefix : prefixs)
796 if (namespace.equals(prefix))
797 return true;
798 return false;
799
800 }
801 return true;
802 }
803
804 private boolean contains(String[] arr, String item) {
805 if (arr != null) {
806 for (String arrItem : arr) {
807 if (StringUtils.equals(arrItem, item))
808 return true;
809 }
810 }
811 return false;
812 }
813
814 static public class SaveActionListener extends EventListener<UIUploadForm> {
815 public void execute(Event<UIUploadForm> event) throws Exception {
816 UIUploadForm uiForm = event.getSource();
817 UIUploadManager uiManager = uiForm.getParent();
818 if(uiForm.getListSameNames(event).size() > 0) {
819 UIPopupWindow uiPopupWindow = uiManager.initPopupWhenHaveSameName();
820 UIUploadBehaviorWithSameName uiUploadBehavior =
821 uiManager.createUIComponent(UIUploadBehaviorWithSameName.class, null, null);
822 uiUploadBehavior.setMessageKey("UIUploadForm.msg.confirm-behavior");
823 uiUploadBehavior.setArguments(
824 uiForm.getListSameNames(event).toArray(new String[uiForm.getListSameNames(event).size()]));
825 uiPopupWindow.setUIComponent(uiUploadBehavior);
826 uiPopupWindow.setShow(true);
827 uiPopupWindow.setRendered(true);
828 event.getRequestContext().addUIComponentToUpdateByAjax(uiManager);
829 return;
830 }
831 uiForm.doUpload(event, false);
832 }
833 }
834
835 static public class CancelActionListener extends EventListener<UIUploadForm> {
836 public void execute(Event<UIUploadForm> event) throws Exception {
837 UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class) ;
838 uiExplorer.cancelAction() ;
839 }
840 }
841
842 static public class RemoveActionListener extends EventListener<UIFormMultiValueInputSet> {
843 public void execute(Event<UIFormMultiValueInputSet> event) throws Exception {
844 UIFormMultiValueInputSet uiSet = event.getSource();
845 UIComponent uiComponent = uiSet.getParent();
846 if (uiComponent instanceof UIUploadForm) {
847 UIUploadForm uiUploadForm = (UIUploadForm)uiComponent;
848 String id = event.getRequestContext().getRequestParameter(OBJECTID);
849 String[] arrayId = id.split(FIELD_LISTTAXONOMY);
850 int index = 0;
851 int indexRemove = 0;
852 if ((arrayId.length > 0) && (arrayId[0].length() > 0))
853 index = new Integer(arrayId[0]).intValue();
854 if ((arrayId.length > 0) && (arrayId[1].length() > 0))
855 indexRemove = new Integer(arrayId[1]).intValue();
856 String idFieldListTaxonomy;
857 if (index == 0)
858 idFieldListTaxonomy = FIELD_LISTTAXONOMY;
859 else
860 idFieldListTaxonomy = index + FIELD_LISTTAXONOMY;
861 if (uiUploadForm.mapTaxonomies.containsKey(idFieldListTaxonomy)) {
862 List<String> indexMapTaxonomy = new ArrayList<String>();
863 indexMapTaxonomy = uiUploadForm.mapTaxonomies.get(idFieldListTaxonomy);
864 uiUploadForm.mapTaxonomies.remove(idFieldListTaxonomy);
865 if (indexMapTaxonomy.size() > indexRemove) indexMapTaxonomy.remove(indexRemove);
866 uiUploadForm.mapTaxonomies.put(idFieldListTaxonomy, indexMapTaxonomy);
867 }
868 uiSet.removeChildById(id);
869 event.getRequestContext().addUIComponentToUpdateByAjax(uiUploadForm);
870 }
871 }
872 }
873
874 static public class AddActionListener extends EventListener<UIFormMultiValueInputSet> {
875 public void execute(Event<UIFormMultiValueInputSet> event) throws Exception {
876 UIFormMultiValueInputSet uiSet = event.getSource();
877 UIUploadForm uiUploadForm = (UIUploadForm) uiSet.getParent();
878 UIApplication uiApp = uiUploadForm.getAncestorOfType(UIApplication.class);
879 try {
880 String fieldTaxonomyId = event.getRequestContext().getRequestParameter(OBJECTID);
881 String[] arrayId = fieldTaxonomyId.split(FIELD_LISTTAXONOMY);
882 int index = 0;
883 if ((arrayId.length > 0) && (arrayId[0].length() > 0)) index = new Integer(arrayId[0]).intValue();
884 String idFieldUpload;
885 if (index == 0) idFieldUpload = FIELD_UPLOAD; else idFieldUpload = index + FIELD_UPLOAD;
886 UIUploadInput uiUploadInput = uiUploadForm.getChildById(idFieldUpload);
887 UploadResource uploadResource = uiUploadInput.getUploadResource(uiUploadInput.getUploadIds()[0]);
888 if (uploadResource == null) {
889 uiApp.addMessage(new ApplicationMessage("UIUploadForm.msg.upload-not-null", null,
890 ApplicationMessage.WARNING));
891
892 return;
893 }
894 UIUploadManager uiUploadManager = uiUploadForm.getParent();
895 UIJCRExplorer uiExplorer = uiUploadForm.getAncestorOfType(UIJCRExplorer.class);
896 String repository = uiExplorer.getRepositoryName();
897
898
899 UIPopupWindow uiPopupWindow = uiUploadManager.initPopupTaxonomy(POPUP_TAXONOMY);
900 UIOneTaxonomySelector uiOneTaxonomySelector =
901 uiUploadManager.createUIComponent(UIOneTaxonomySelector.class, null, null);
902 uiPopupWindow.setUIComponent(uiOneTaxonomySelector);
903 TaxonomyService taxonomyService = uiUploadForm.getApplicationComponent(TaxonomyService.class);
904 List<Node> lstTaxonomyTree = taxonomyService.getAllTaxonomyTrees();
905 if (lstTaxonomyTree.size() == 0) throw new AccessDeniedException();
906 String workspaceName = lstTaxonomyTree.get(0).getSession().getWorkspace().getName();
907 uiOneTaxonomySelector.setIsDisable(workspaceName, false);
908 uiOneTaxonomySelector.setRootNodeLocation(repository, workspaceName, lstTaxonomyTree.get(0).getPath());
909 uiOneTaxonomySelector.setExceptedNodeTypesInPathPanel(new String[] {Utils.EXO_SYMLINK});
910 uiOneTaxonomySelector.init(uiExplorer.getSystemProvider());
911 String param = "returnField=" + fieldTaxonomyId;
912 uiOneTaxonomySelector.setSourceComponent(uiUploadForm, new String[]{param});
913 uiPopupWindow.setRendered(true);
914 uiPopupWindow.setShow(true);
915 event.getRequestContext().addUIComponentToUpdateByAjax(uiUploadManager);
916 } catch (AccessDeniedException accessDeniedException) {
917 uiApp.addMessage(new ApplicationMessage("Taxonomy.msg.AccessDeniedException", null,
918 ApplicationMessage.WARNING));
919
920 return;
921 } catch (Exception e) {
922 JCRExceptionManager.process(uiApp, e);
923
924 return;
925 }
926 }
927 }
928
929 static public class AddUploadActionListener extends EventListener<UIUploadForm> {
930 public void execute(Event<UIUploadForm> event) throws Exception {
931 UIUploadForm uiUploadForm = event.getSource();
932 List<UIComponent> listChildren = uiUploadForm.getChildren();
933 int index = 0;
934 int numberUploadFile = 0;
935 String fieldFieldUpload = null;
936 for (UIComponent uiComp : listChildren) {
937 if(uiComp instanceof UIUploadInput) {
938 fieldFieldUpload = uiComp.getId();
939 numberUploadFile++;
940 }
941 }
942 if (fieldFieldUpload != null) {
943 String[] arrayId = fieldFieldUpload.split(FIELD_UPLOAD);
944 if ((arrayId.length > 0) && (arrayId[0].length() > 0)) index = new Integer(arrayId[0]).intValue();
945 }
946 index++;
947 uiUploadForm.addUIFormInput(new UIFormStringInput(index + FIELD_NAME, index + FIELD_NAME, null));
948 PortletRequestContext pcontext = (PortletRequestContext)WebuiRequestContext.getCurrentInstance();
949 PortletPreferences portletPref = pcontext.getRequest().getPreferences();
950 String limitPref = portletPref.getValue(Utils.UPLOAD_SIZE_LIMIT_MB, "");
951 UIUploadInput uiInput = null;
952 if (limitPref != null) {
953 try {
954 uiInput = new UIUploadInput(index + FIELD_UPLOAD,
955 index + FIELD_UPLOAD,
956 Integer.parseInt(limitPref.trim()));
957 } catch (NumberFormatException e) {
958 uiInput = new UIUploadInput(index + FIELD_UPLOAD, index + FIELD_UPLOAD);
959 }
960 } else {
961 uiInput = new UIUploadInput(index + FIELD_UPLOAD, index + FIELD_UPLOAD);
962 }
963 uiUploadForm.addUIFormInput(uiInput);
964 UIFormMultiValueInputSet uiFormMultiValue = uiUploadForm.createUIComponent(UIFormMultiValueInputSet.class,
965 "UploadMultipleInputset",
966 null);
967 uiFormMultiValue.setId(index + FIELD_LISTTAXONOMY);
968 uiFormMultiValue.setName(index + FIELD_LISTTAXONOMY);
969 uiFormMultiValue.setType(UIFormStringInput.class);
970 uiFormMultiValue.setEditable(false);
971 uiUploadForm.addUIFormInput(uiFormMultiValue);
972 uiUploadForm.setNumberUploadFile(numberUploadFile + 1);
973 uiUploadForm.setRendered(true);
974 event.getRequestContext().addUIComponentToUpdateByAjax(uiUploadForm.getParent());
975 }
976 }
977
978 static public class RemoveUploadActionListener extends EventListener<UIUploadForm> {
979 public void execute(Event<UIUploadForm> event) throws Exception {
980 String id = event.getRequestContext().getRequestParameter(OBJECTID);
981 UIUploadForm uiUploadForm = event.getSource();
982 List<UIComponent> listChildren = uiUploadForm.getChildren();
983 int index = 0;
984 for (UIComponent uiComp : listChildren) {
985 if(uiComp instanceof UIUploadInput) index++;
986 }
987 String[] arrayId = id.split(FIELD_NAME);
988 int indexRemove = 0;
989 if ((arrayId.length > 0) && (arrayId[0].length() > 0))
990 indexRemove = new Integer(arrayId[0]).intValue();
991 if (indexRemove == 0) {
992 uiUploadForm.removeChildById(FIELD_NAME);
993 uiUploadForm.removeChildById(FIELD_UPLOAD);
994 uiUploadForm.removeChildById(FIELD_LISTTAXONOMY);
995 if (uiUploadForm.mapTaxonomies.containsKey(FIELD_LISTTAXONOMY))
996 uiUploadForm.mapTaxonomies.remove(FIELD_LISTTAXONOMY);
997 } else {
998 uiUploadForm.removeChildById(indexRemove + FIELD_NAME);
999 uiUploadForm.removeChildById(indexRemove + FIELD_UPLOAD);
1000 uiUploadForm.removeChildById(indexRemove + FIELD_LISTTAXONOMY);
1001 if (uiUploadForm.mapTaxonomies.containsKey(indexRemove + FIELD_LISTTAXONOMY))
1002 uiUploadForm.mapTaxonomies.remove(indexRemove + FIELD_LISTTAXONOMY);
1003 }
1004 uiUploadForm.setNumberUploadFile(index - 1);
1005 uiUploadForm.setRendered(true);
1006 event.getRequestContext().addUIComponentToUpdateByAjax(uiUploadForm.getParent());
1007 }
1008 }
1009 }