001: package ru.emdev.EmForge.wiki.web.bean;
002:
003: import java.io.FileInputStream;
004: import java.io.IOException;
005: import java.io.InputStream;
006:
007: import javax.faces.component.UIParameter;
008:
009: import org.apache.commons.io.IOUtils;
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.emforge.EmForgeException;
013: import org.emforge.xfer.AttachmentHistoryTO;
014: import org.emforge.xfer.AttachmentTO;
015: import org.richfaces.event.UploadEvent;
016: import org.richfaces.model.UploadItem;
017: import org.springframework.beans.factory.InitializingBean;
018:
019: import ru.emdev.EmForge.web.bean.MainMenuController.MainMenuItem;
020:
021: public class AttachmentInfoController extends BaseWikiControllerImpl
022: implements InitializingBean {
023: protected final Log logger = LogFactory.getLog(getClass());
024:
025: private String m_fileName;
026:
027: @Override
028: protected void init() {
029: m_pageName = getPageParam();
030: m_fileName = getParam("attachment");
031: }
032:
033: @Override
034: public MainMenuItem getSelectionItemOnMainMenu() {
035: return null;
036: }
037:
038: @Override
039: public String getTitleImpl() {
040: return m_fileName;
041: }
042:
043: @Override
044: public Crumb getTrailCrumbInfo() {
045: return new Crumb(m_fileName, "attachInfo/" + m_pageName + "/"
046: + m_fileName);
047: }
048:
049: public void setAttachmentName(String i_name) {
050: m_fileName = i_name;
051: }
052:
053: public String getAttachmentName() {
054: return m_fileName;
055: }
056:
057: public String getViewUrl() {
058: return m_wikiEngine.getViewURL(m_pageName);
059: }
060:
061: public String getViewAttachUrl() {
062: return "attach/" + m_pageName + "/" + m_fileName;
063: }
064:
065: public AttachmentTO getAttachment() {
066: try {
067: return m_attachmentService.getAttachment(m_pageName,
068: m_fileName);
069: } catch (Exception ex) {
070: logger.error("Cannot get attachment", ex);
071: return null;
072: }
073: }
074:
075: public String getFullAttachName() {
076: return m_pageName + "/" + m_fileName;
077: }
078:
079: public String deleteAttachment() {
080: AttachmentTO att = getAttachment();
081: String pageName = att.getParentName();
082:
083: try {
084: m_attachmentService.deleteAttachment(att);
085: redirect(m_wikiEngine.getViewURL(m_pageName));
086: } catch (EmForgeException e) {
087: logger.error("Cannot delete attachment", e);
088: return null;
089: }
090:
091: redirect("wiki/" + pageName);
092: return null;
093: }
094:
095: @SuppressWarnings("unchecked")
096: public AttachmentHistoryTO[] getRevisionList() {
097: try {
098: AttachmentTO att = new AttachmentTO();
099: att.setParentName(m_pageName);
100: att.setFileName(m_fileName);
101: return m_attachmentService.getAttachmentHistory(att);
102: } catch (EmForgeException e) {
103: logger.error("Cannot get attachment version history", e);
104: }
105: return new AttachmentHistoryTO[0];
106: }
107:
108: /**
109: * @return <code>True</code> if the current user is a writer, otherwise
110: * <code>False</code>
111: */
112: public boolean isCanDeleteAttachment() {
113: return m_attachmentService.canDeleteAttachment(getAttachment());
114: }
115:
116: public void upload(UploadEvent event) throws IOException {
117: try {
118: UploadItem item = event.getUploadItem();
119:
120: // get page name
121: for (Object uiObject : event.getComponent().getChildren()) {
122: if (uiObject instanceof UIParameter) {
123: final UIParameter param = (UIParameter) uiObject;
124: if (param.getName().equals("page")) {
125: m_pageName = param.getValue().toString();
126: } else if (param.getName().equals("attachment")) {
127: m_fileName = param.getValue().toString();
128: }
129: }
130: }
131:
132: InputStream is = new FileInputStream(item.getFile());
133: byte[] content = IOUtils.toByteArray(is);
134: m_attachmentService.addAttachment(m_pageName, m_fileName,
135: content, null);
136: } catch (Exception pex) {
137: logger.error("Cannot upload file", pex);
138: }
139: }
140:
141: }
|