001: package com.sun.portal.app.filesharing.faces;
002:
003: import com.sun.portal.app.filesharing.repo.RepoException;
004: import com.sun.portal.app.filesharing.repo.RepoFactory;
005: import com.sun.portal.app.filesharing.repo.RepoItem;
006: import com.sun.portal.app.filesharing.repo.Repository;
007: import com.sun.portal.app.filesharing.servlet.FileDownloadServlet;
008: import com.sun.portal.app.filesharing.util.InfoResolver;
009: import com.sun.portal.app.filesharing.util.InfoResolverFactory;
010: import com.sun.portal.app.filesharing.util.LogUtils;
011: import com.sun.web.ui.component.FileChooser;
012: import com.sun.web.ui.component.Listbox;
013: import com.sun.web.ui.model.ResourceItem;
014: import com.sun.web.ui.model.ResourceModel;
015: import com.sun.web.ui.model.UploadedFile;
016:
017: import javax.faces.context.ExternalContext;
018: import javax.faces.context.FacesContext;
019: import javax.faces.event.AbortProcessingException;
020: import javax.faces.event.ValueChangeEvent;
021: import java.io.IOException;
022: import java.io.Serializable;
023: import java.util.Locale;
024:
025: import java.util.ResourceBundle;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028:
029: import javax.portlet.*;
030:
031: /**
032: * @author Alejandro Abdelnur
033: */
034: public class FolderBean implements Serializable {
035: private transient FileChooser _fileChooser;
036: ResourceModel _model;
037:
038: // Action attributes, to be cleaned up after an action is done or canceled
039: //
040: private ResourceItem _selectedItem = null;
041: private String _selectedField = null;
042: private transient UploadedFile _uploadedFile;
043:
044: private String _repositoryId;
045: private Repository _repository;
046:
047: private String _user;
048:
049: private static Logger _logger = LogUtils.getInstance().getLogger(
050: FolderBean.class.getName());
051: private ResourceBundle rb = null;
052:
053: public FolderBean() throws Exception {
054: if (_logger.isLoggable(Level.FINER)) {
055: _logger.log(Level.FINER, "bean.<init>");
056: }
057: FacesContext facesContext = FacesContext.getCurrentInstance();
058:
059: InfoResolver _info = InfoResolverFactory.getFactory()
060: .getInfoResolver(facesContext);
061: _user = _info.getUserId();
062: _repositoryId = _info.getRepositoryId();
063:
064: _repository = RepoFactory.getRepository(facesContext,
065: _repositoryId, _user);
066: FacesContext context = FacesContext.getCurrentInstance();
067: Locale locale = context.getViewRoot().getLocale();
068: rb = ResourceBundle.getBundle(
069: "com.sun.portal.app.filesharing.bundle.filesharing",
070: locale);
071: _model = new RepositoryFCModel(_repository, _repositoryId,
072: _user, this );
073: }
074:
075: // we are creating a FileChooser instance because the current tag does not support
076: // setting a custom model nor the 'fileAndFolderChooser' property.
077:
078: public void setFileChooser(FileChooser fileChooser) {
079: if (_logger.isLoggable(Level.FINER)) {
080: _logger.log(Level.FINER, "bean.setFileChooser(): "
081: + _fileChooser);
082: }
083: _fileChooser = fileChooser;
084: //_fileChooser.setFileAndFolderChooser(true);
085: //_fileChooser.setModel(_model);
086: //if (_selectedItem!=null) {
087: // _fileChooser.setSelected(_selectedItem);
088: //}
089: }
090:
091: public FileChooser getFileChooser() {
092: if (_logger.isLoggable(Level.FINER)) {
093: _logger.log(Level.FINER, "bean.getFileChooser()");
094: }
095: if (_fileChooser == null) {
096: _fileChooser = new FileChooser();
097: _fileChooser.setFileAndFolderChooser(true);
098: _fileChooser.setModel(_model);
099: if (_selectedItem != null) {
100: _fileChooser.setSelected(_selectedItem);
101: }
102: }
103: return _fileChooser;
104: }
105:
106: public String getLocalizedMessage(String key) {
107: if (rb != null) {
108: return rb.getString(key);
109: }
110: return key;
111: }
112:
113: String getLocalizedErrorMessage(String type, String key) {
114: return getLocalizedMessage(type + "." + key);
115: }
116:
117: public ResourceModel getModel() {
118: if (_logger.isLoggable(Level.FINER)) {
119: _logger.log(Level.FINER, "bean.getModel(): " + _model);
120: }
121: return _model;
122: }
123:
124: public void setCommunityName(String communityName) {
125: }
126:
127: public String getFileUrlTemplate() {
128: ExternalContext extCtx = FacesContext.getCurrentInstance()
129: .getExternalContext();
130: // TODO, nasty, it should be encapsulated the COMMUNITY_ID_PARAM
131: String url = extCtx.getRequestContextPath() + "/file/{0}?"
132: + FileDownloadServlet.FILE_PARAM + "={1}&"
133: + InfoResolverFactory.REPOSITORY_ID_PARAM + "="
134: + _repositoryId;
135: if (_logger.isLoggable(Level.FINER)) {
136: _logger.log(Level.FINER, "bean.getFileUrlTemplate(): "
137: + url);
138: }
139: return url;
140: }
141:
142: public void setFilterField(String value) {
143: String filter = _model.getFilterValue();
144: if (value != null && filter != null && !value.equals(filter)) {
145: _model.setFilterValue(value);
146: }
147: }
148:
149: public String getFilterField() {
150: return _model.getFilterValue();
151: }
152:
153: private String unescaped(String s) {
154: if (s == null)
155: return s;
156: char[] chars = s.toCharArray();
157: StringBuffer sb = new StringBuffer(chars.length);
158: for (int i = 0; i < chars.length; i++) {
159: if (chars[i] == '\\') {
160: i++;
161: }
162: sb.append(chars[i]);
163: }
164: return sb.toString();
165: }
166:
167: public void setSelectedField(String value) {
168: _selectedField = unescaped(value);
169: if (_selectedField != null) {
170: if (_model.getCurrentDir().equals(RepoItem.ROOT.getPath())) {
171: _selectedPath = _model.getCurrentDir() + _selectedField;
172: } else {
173: _selectedPath = _model.getCurrentDir()
174: + _model.getSeparatorString() + _selectedField;
175: }
176: }
177: }
178:
179: public String getSelectedField() {
180: return _selectedField;
181: }
182:
183: public void setSelected(Object selected) {
184: // doing the check for array because at this point it is not clear
185: // what the FC returns under different situations.
186: if (selected != null && selected instanceof Object[]) {
187: selected = ((Object[]) selected)[0];
188: }
189: if (_logger.isLoggable(Level.FINER)) {
190: _logger.log(Level.FINER, "bean.setSelected(" + selected
191: + ")");
192: }
193: _selectedItem = (ResourceItem) selected;
194: }
195:
196: public Object getSelected() {
197: if (_logger.isLoggable(Level.FINER)) {
198: _logger.log(Level.FINER, "bean.getSelectedItem(): "
199: + _selectedItem);
200: }
201: return _selectedItem;
202: }
203:
204: public UploadedFile getUploadedFile() {
205: if (_logger.isLoggable(Level.FINER)) {
206: _logger.log(Level.FINER, "bean.getUploadedFile(): "
207: + _uploadedFile);
208: }
209: return _uploadedFile;
210: }
211:
212: public void setUploadedFile(UploadedFile uploadedFile) {
213: if (_logger.isLoggable(Level.FINER)) {
214: _logger.log(Level.FINER, "bean.setUploadedFile("
215: + uploadedFile + ")");
216: }
217: _uploadedFile = uploadedFile;
218: }
219:
220: public String getSelectedName() {
221: if (_logger.isLoggable(Level.FINER)) {
222: _logger.log(Level.FINER, "bean.getSelectedName(): "
223: + getItemName(_selectedItem));
224: }
225: if (_selectedItem != null) {
226: return getItemName(_selectedItem);
227: }
228: return _selectedField;
229: }
230:
231: private String getItemName(ResourceItem rItem) {
232: String s = null;
233: if (rItem != null) {
234: RepoItem repoItem = (RepoItem) rItem.getItemValue();
235: s = repoItem.getName();
236: }
237: return s;
238: }
239:
240: public String getCurrentDir() {
241: if (_logger.isLoggable(Level.FINER)) {
242: _logger.log(Level.FINER, "bean.getCurrentDir(): "
243: + _model.getCurrentDir());
244: }
245: clearAlert();
246: return _model.getCurrentDir();
247: }
248:
249: public void setCurrentDir(String currDir) {
250: if (_logger.isLoggable(Level.FINER)) {
251: _logger.log(Level.FINER, "bean.getCurrentDir(): "
252: + _model.getCurrentDir());
253: }
254: _model.setCurrentDir(currDir);
255: }
256:
257: public boolean getIsRoot() {
258: String currDir = _model.getCurrentDir();
259: return (currDir != null && currDir.equals("/"));
260: }
261:
262: public void fileChooserChangeListener(ValueChangeEvent event)
263: throws AbortProcessingException {
264: clearAlert();
265: }
266:
267: // REMOVE
268:
269: public String remove() {
270: if (_logger.isLoggable(Level.FINER)) {
271: _logger.log(Level.FINER, "bean.remove()");
272: }
273: clearAlert();
274: if (_selectedItem != null || _selectedPath != null) {
275: try {
276: RepoItem repoItem;
277: if (_selectedItem != null) {
278: repoItem = (RepoItem) _selectedItem.getItemValue();
279: } else {
280: repoItem = new RepoItem(_selectedPath, _user);
281: repoItem = _repository.getItemInfo(repoItem);
282: }
283: if (repoItem.isDirectory()) {
284: _repository.deleteFolder(repoItem, false);
285: } else {
286: _repository.deleteFile(repoItem);
287: }
288: _selectedItem = null;
289: cleanActionState();
290: } catch (RepoException rex) {
291: _logger.log(Level.WARNING, "remove()", rex);
292: setAlert(getLocalizedErrorMessage("repository", rex
293: .getId()));
294: } catch (Exception ex) {
295: _logger.log(Level.WARNING, "remove()", ex);
296: setAlert(getLocalizedErrorMessage("repository", "S000"));
297: }
298: } else {
299: _logger.log(Level.WARNING, "remove() no selected item");
300: setAlert(getLocalizedErrorMessage("ui", "UI000"));
301: }
302: return "done";
303: }
304:
305: // MOVE
306:
307: private ResourceItem _itemToMove;
308: private String _selectedPath;
309: private String _pathToMove;
310:
311: public String getSelectedPath() {
312: return _selectedPath;
313: }
314:
315: public String prepareMove() {
316: if (_logger.isLoggable(Level.FINER)) {
317: _logger.log(Level.FINER, "bean.prepareMove()");
318: }
319: clearAlert();
320: String status;
321: if (_selectedItem == null && _selectedField == null) {
322: setAlert(getLocalizedErrorMessage("ui", "UI010")); // TODO LOG ME
323: status = "error";
324: } else {
325: if (_selectedItem != null) {
326: _itemToMove = _selectedItem;
327: } else {
328: _itemToMove = _fileChooser.getModel().getResourceItem(
329: _selectedPath);
330: }
331: clearFileChooserSelection();
332: status = "move";
333: _fileChooser.setFileAndFolderChooser(false);
334: _fileChooser.setFolderChooser(true);
335: }
336: return status;
337: }
338:
339: public String getItemToMove() {
340: if (_logger.isLoggable(Level.FINER)) {
341: _logger.log(Level.FINER, "bean.getItemToMove(): "
342: + _itemToMove);
343: }
344: if (_itemToMove != null) {
345: return getItemName(_itemToMove);
346: }
347: return _pathToMove;
348: }
349:
350: public String move() {
351: if (_logger.isLoggable(Level.FINER)) {
352: _logger.log(Level.FINER, "bean.move()");
353: }
354: clearAlert();
355: String status;
356: if (_itemToMove != null || _pathToMove != null) {
357: try {
358: RepoItem folder = null;
359: if (_selectedItem != null) {
360: folder = (RepoItem) _selectedItem.getItemValue();
361: folder = (folder.isDirectory()) ? folder : null;
362: clearFileChooserSelection();
363: } else if (_selectedField != null) {
364: folder = new RepoItem(_selectedPath, _user);
365: folder = _repository.getItemInfo(folder);
366: folder = (folder.isDirectory()) ? folder : null;
367: clearFileChooserSelection();
368: }
369: if (folder == null) {
370: folder = new RepoItem(_model.getCurrentDir(), _user);
371: }
372: RepoItem itemToMove = null;
373: if (_itemToMove != null) {
374: itemToMove = (RepoItem) _itemToMove.getItemValue();
375: } else {
376: itemToMove = new RepoItem(_pathToMove, _user);
377: }
378: _repository.move(itemToMove, folder);
379: cleanActionState();
380: status = "done";
381: } catch (RepoException rex) {
382: setAlert(getLocalizedErrorMessage("repository", rex
383: .getId())); // TODO LOG ME
384: status = "error";
385: } catch (Exception ex) {
386: setAlert(getLocalizedErrorMessage("repository", "S010")); // TODO LOG ME
387: status = "error";
388: }
389: } else {
390: setAlert(getLocalizedErrorMessage("ui", "UI011")); // TODO LOG ME
391: status = "error";
392: }
393: return status;
394: }
395:
396: // COPY
397:
398: private ResourceItem _itemToCopy;
399: private String _pathToCopy;
400:
401: public String prepareCopy() {
402: if (_logger.isLoggable(Level.FINER)) {
403: _logger.log(Level.FINER, "bean.prepareCopy()");
404: }
405: clearAlert();
406: String status;
407: if (_selectedItem == null && _selectedField == null) {
408: setAlert(getLocalizedErrorMessage("ui", "UI020")); // TODO LOG ME
409: status = "error";
410: } else {
411: _itemToCopy = _selectedItem;
412: if (_selectedField != null) {
413: _pathToCopy = _selectedPath;
414: }
415: clearFileChooserSelection();
416: status = "copy";
417: _fileChooser.setFileAndFolderChooser(false);
418: _fileChooser.setFolderChooser(true);
419: }
420: return status;
421: }
422:
423: public String getItemToCopy() {
424: if (_logger.isLoggable(Level.FINER)) {
425: _logger.log(Level.FINER, "bean.getItemToCopy(): "
426: + getItemName(_itemToCopy));
427: }
428: if (_itemToCopy != null) {
429: return getItemName(_itemToCopy);
430: }
431: return _pathToCopy;
432: }
433:
434: private void clearFileChooserSelection() {
435: _selectedItem = null;
436: _selectedField = null;
437: Listbox lb = (Listbox) _fileChooser.getListComponent();
438: lb.setSelected(null);
439: }
440:
441: public String copy() {
442: if (_logger.isLoggable(Level.FINER)) {
443: _logger.log(Level.FINER, "bean.copy()");
444: }
445: clearAlert();
446: String status;
447: if (_itemToCopy != null || _pathToCopy != null) {
448: try {
449: RepoItem folder = null;
450: if (_selectedItem != null) {
451: folder = (RepoItem) _selectedItem.getItemValue();
452: folder = (folder.isDirectory()) ? folder : null;
453: clearFileChooserSelection();
454: } else if (_selectedField != null) {
455: folder = new RepoItem(_selectedPath, _user);
456: folder = _repository.getItemInfo(folder);
457: folder = (folder.isDirectory()) ? folder : null;
458: clearFileChooserSelection();
459: }
460: if (folder == null) {
461: folder = new RepoItem(_model.getCurrentDir(), _user);
462: }
463: RepoItem itemToCopy;
464: if (_itemToCopy != null) {
465: itemToCopy = (RepoItem) _itemToCopy.getItemValue();
466: } else {
467: itemToCopy = new RepoItem(_pathToCopy, _user);
468: }
469: _repository.copy(itemToCopy, folder);
470: cleanActionState();
471: status = "done";
472: } catch (RepoException rex) {
473: setAlert(getLocalizedErrorMessage("repository", rex
474: .getId())); // TODO LOG ME
475: status = "error";
476: } catch (Exception ex) {
477: setAlert(getLocalizedErrorMessage("repository", "S020")); // TODO LOG ME
478: status = "error";
479: }
480: } else {
481: setAlert(getLocalizedErrorMessage("ui", "UI021")); // TODO LOG ME
482: status = "error";
483: }
484: return status;
485: }
486:
487: // RENAME
488:
489: public String prepareRename() {
490: if (_logger.isLoggable(Level.FINER)) {
491: _logger.log(Level.FINER, "bean.prepareRename()");
492: }
493: clearAlert();
494: String status;
495: if (_selectedItem == null && _selectedPath == null) {
496: setAlert(getLocalizedErrorMessage("ui", "UI030")); // TODO LOG ME
497: status = "error";
498: } else {
499: status = "rename";
500: }
501: return status;
502: }
503:
504: private String _newName;
505:
506: public void setNewName(String name) {
507: _newName = name;
508: }
509:
510: public String getNewName() {
511: return _newName;
512: }
513:
514: public String rename() {
515: if (_logger.isLoggable(Level.FINER)) {
516: _logger.log(Level.FINER, "bean.rename()");
517: }
518: clearAlert();
519: String status;
520: if (_newName != null) {
521: if (_selectedItem != null || _selectedPath != null) {
522: try {
523: RepoItem repoItem;
524: if (_selectedItem != null) {
525: repoItem = (RepoItem) _selectedItem
526: .getItemValue();
527: } else {
528: repoItem = new RepoItem(_selectedPath, _user);
529: }
530: clearFileChooserSelection();
531:
532: _repository.rename(repoItem, _newName);
533: cleanActionState();
534: status = "done";
535: } catch (RepoException rex) {
536: setAlert(getLocalizedErrorMessage("repository", rex
537: .getId())); // TODO LOG ME
538: status = "error";
539: } catch (Exception ex) {
540: setAlert(getLocalizedErrorMessage("repository",
541: "S031")); // TODO LOG ME
542: status = "error";
543: }
544: } else {
545: setAlert(getLocalizedErrorMessage("ui", "UI031")); // TODO LOG ME
546: status = "error";
547: }
548: } else {
549: setAlert(getLocalizedErrorMessage("ui", "UI032")); // TODO LOG ME
550: status = "error";
551: }
552: return status;
553: }
554:
555: // ADD FILE
556:
557: public String prepareAddFile() {
558: if (_logger.isLoggable(Level.FINER)) {
559: _logger.log(Level.FINER, "bean.prepareAddFile()");
560: }
561: clearAlert();
562: return "addFile";
563: }
564:
565: private String _newFile;
566:
567: public void setNewFile(String file) {
568: _newFile = file;
569: }
570:
571: public String getNewFile() {
572: return _newFile;
573: }
574:
575: public String addFile() {
576: if (_logger.isLoggable(Level.FINER)) {
577: _logger.log(Level.FINER, "bean.addFile()");
578: }
579: clearAlert();
580: String status;
581: if (_newFile != null) {
582: if (_uploadedFile != null) {
583: Object req = FacesContext.getCurrentInstance()
584: .getExternalContext().getRequest();
585: if (req instanceof PortletRequest) {
586: Exception e = (Exception) ((PortletRequest) req)
587: .getAttribute("_upload_exception_");
588: }
589: try {
590: RepoItem item = new RepoItem(
591: _model.getCurrentDir(), "dummy");
592: if (_uploadedFile.getSize() > 0) {
593: item = new RepoItem(item, _newFile, _user,
594: _uploadedFile.getSize());
595: _repository.addFile(item, _uploadedFile
596: .getInputStream());
597: cleanActionState();
598: status = "done";
599: } else {
600: setAlert(getLocalizedMessage("uploademptyfile"));
601: status = "error";
602: }
603: } catch (RepoException rex) {
604: setAlert(getLocalizedErrorMessage("repository", rex
605: .getId())); // TODO LOG ME
606: status = "error";
607: } catch (IOException ioex) {
608: setAlert(getLocalizedErrorMessage("repository",
609: "R040")); // TODO LOG ME
610: status = "error";
611: } catch (Exception ex) {
612: setAlert(getLocalizedErrorMessage("repository",
613: "R041")); // TODO LOG ME
614: status = "error";
615: }
616: } else {
617: setAlert(getLocalizedErrorMessage("ui", "UI040")); // TODO LOG ME
618: status = "error";
619: }
620: } else {
621: _showAlert = true;
622: _alertType = "error";
623: setAlert(getLocalizedErrorMessage("ui", "UI041")); // TODO LOG ME
624: status = "error";
625: }
626: _uploadedFile = null;
627: return status;
628: }
629:
630: // ADD FOLDER
631:
632: private String _newFolder;
633:
634: public void setNewFolder(String folder) {
635: _newFolder = folder;
636: }
637:
638: public String getNewFolder() {
639: return _newFolder;
640: }
641:
642: public String prepareCreateFolder() {
643: if (_logger.isLoggable(Level.FINER)) {
644: _logger.log(Level.FINER, "bean.prepareCreateFolder()");
645: }
646: clearAlert();
647: return "createFolder";
648: }
649:
650: public String createFolder() {
651: if (_logger.isLoggable(Level.FINER)) {
652: _logger.log(Level.FINER, "bean.createFolder()");
653: }
654: clearAlert();
655: String status;
656: if (_newFolder != null) {
657: try {
658: RepoItem item = new RepoItem(_model.getCurrentDir(),
659: "dummy");
660: item = new RepoItem(item, _newFolder, _user);
661: _repository.addFolder(item);
662: cleanActionState();
663: status = "done";
664: } catch (RepoException rex) {
665: setAlert(getLocalizedErrorMessage("repository", rex
666: .getId())); // TODO LOG ME
667: status = "error";
668: } catch (Exception ex) {
669: setAlert(getLocalizedErrorMessage("repository", "R050")); // TODO LOG ME
670: status = "error";
671: }
672: } else {
673: setAlert(getLocalizedErrorMessage("ui", "UI050")); // TODO LOG ME
674: status = "error";
675: }
676: return status;
677: }
678:
679: // CANCEL
680:
681: public String cancel() {
682: if (_logger.isLoggable(Level.FINER)) {
683: _logger.log(Level.FINER, "bean.cancel()");
684: }
685: cleanActionState();
686: return "done";
687: }
688:
689: private void cleanActionState() {
690: if (_logger.isLoggable(Level.FINER)) {
691: _logger.log(Level.FINER, "bean.cleanActionState()");
692: }
693: _newFolder = null;
694: _newFile = null;
695: _newName = null;
696: _uploadedFile = null;
697: _itemToCopy = null;
698: _itemToMove = null;
699: if (_fileChooser != null) {
700: _fileChooser.setFileAndFolderChooser(true);
701: }
702: clearAlert();
703: }
704:
705: // ALERTS
706:
707: private String _alertType;
708: private String _alertSummary;
709: private boolean _showAlert;
710:
711: public String getAlertType() {
712: return _alertType;
713: }
714:
715: public String getAlertSummary() {
716: return _alertSummary;
717: }
718:
719: private void getUploadError() {
720: Object req = FacesContext.getCurrentInstance()
721: .getExternalContext().getRequest();
722: if (req instanceof PortletRequest) {
723:
724: Exception e = (Exception) ((PortletRequest) req)
725: .getPortletSession().getAttribute(
726: "_upload_exception_");
727: if (e != null) {
728: if (e.getClass().getName().endsWith(
729: "SizeLimitExceededException")) {
730: setAlert(getLocalizedMessage("addFileExceedSizeLimitAlert"));
731: } else {
732: setAlert(e.getLocalizedMessage());
733: }
734: ((PortletRequest) req).getPortletSession()
735: .removeAttribute("_upload_exception_");
736: }
737: }
738: }
739:
740: public boolean getShowAlert() {
741: if (!_showAlert) {
742: getUploadError();
743: }
744: return _showAlert;
745: }
746:
747: void setAlert(String msg) {
748: _showAlert = true;
749: _alertType = "error";
750: _alertSummary = msg;
751: }
752:
753: void clearAlert() {
754: if (_logger.isLoggable(Level.FINER)) {
755: _logger.log(Level.FINER, "bean.clearAlert()");
756: }
757: _showAlert = false;
758: _alertType = null;
759: _alertSummary = null;
760: }
761:
762: }
|