001: package de.webman.content.eventhandler;
002:
003: import com.teamkonzept.web.*;
004: import com.teamkonzept.webman.*;
005: import com.teamkonzept.webman.db.TKWebmanDBManager;
006: import com.teamkonzept.webman.mainint.*;
007: import com.teamkonzept.webman.mainint.db.*;
008: import com.teamkonzept.webman.mainint.db.queries.*;
009: import com.teamkonzept.webman.mainint.events.*;
010: import com.teamkonzept.lib.*;
011: import com.teamkonzept.field.*;
012: import com.teamkonzept.field.db.*;
013: import com.teamkonzept.db.*;
014: import com.teamkonzept.publishing.markups.*;
015: import org.apache.log4j.Category;
016: import de.webman.acl.Policy;
017: import java.sql.*;
018: import java.io.*;
019:
020: /**
021: * Modifes either a new or an existing content.
022: *
023: * @author $Author: alex $
024: * @version $Revision: 1.14.2.1 $
025: */
026: public class CEExtModifyHandler extends DefaultEventHandler implements
027: ParameterTypes, FrameConstants, DatabaseDefaults,
028: QueryConstants {
029:
030: /** for Logging */
031: // private static Category cat = Category.getInstance(CEExtModifyHandler.class);
032: /** Action command */
033: static final String DELETE = "DELETE";
034:
035: static final String EXT_MODIFY = "EXT_MODIFY";
036: static final String TARGET = "TARGET";
037: /** Action command */
038: static final String UPLOAD = "UPLOAD";
039:
040: static final String CE_EXT_MODIFY = "CE_EXT_MODIFY";
041:
042: /**
043: * The singleton instance.
044: */
045: private static final CEExtModifyHandler INSTANCE = new CEExtModifyHandler();
046:
047: /**
048: * Prevents outside instantiation.
049: */
050: private CEExtModifyHandler() {
051: // NOP
052: }
053:
054: /**
055: * Returns the singleton instance of this handler.
056: */
057: public static CEExtModifyHandler getInstance() {
058: return INSTANCE;
059: }
060:
061: /**
062: * Handles the specified event.
063: *
064: * @param evt the event to be handled.
065: * @throws TKException if any error occurred during event handling.
066: */
067: public void handleEvent(TKEvent evt) throws TKException {
068: try {
069: CEUtils.checkEvent(evt);
070: TKHashtable params = evt.getParams().getClass(PARAMETER);
071:
072: TKHTMLTemplate t;// = evt.getPrepHTMLTemplate( "ce_upload.tmpl" );
073:
074: ContentContext ceContext = new ContentContext(evt
075: .getParams(), evt.getName());
076: TKFormDBData data = new TKFormDBData(ceContext.formId
077: .intValue());
078: TKFormDBInterface.Get(data);
079: TKBaseField field = TKFieldRegistry.getFieldFromDB(data);
080:
081: String action = evt.getParameter(ACTION, EXT_MODIFY);
082: String target = evt.getParameter(TARGET, EXT_MODIFY);
083: //---- ACTION: SHOWFILE ----//
084: if (target == null || target.equals(""))
085: target = evt.getParameter(PARAMETER, TARGET);
086:
087: TKUploadField extField = (TKUploadField) field.getTarget(
088: target, "");
089:
090: if (action != null && action.equals(UPLOAD)) {
091:
092: t = evt.getPrepHTMLTemplate("ce_fields.tmpl");
093: // Content auf geändert setzten
094: t.set("CHANGED", new Integer(1));
095: TKUploadFileInputStream fileInputStr = ((TKUploadFileInputStream) params
096: .get(TKUploadField.GETFILENAMESTREAM));
097: File tmpFile = fileInputStr.file;
098: /* TKUploadField eventually modifies NEW_FILENAME */
099: String filename = evt.getParameter(PARAMETER,
100: TKUploadField.NEW_FILENAME);
101: if (filename == null || filename.equals("")) {
102: filename = extField
103: .splitBrowserFileName(fileInputStr
104: .getUploadFileName());
105: }
106: filename = TKUploadField.checkFileName(filename);
107: TKHashtable header = fileInputStr.getUploadHeader();
108: String contentType = TKUploadField
109: .fixContentType(header.get(
110: TKUploadField.CONTENT_TYPE).toString());
111:
112: // Mimetype checken !
113: if (!extField.compareMimes(contentType)) {
114: Object[] args = { contentType };
115: throw new TKUserException(
116: "Mime Typen matchen nicht",
117: NOT_MATCHING_MIMETYPES, USER_SEVERITY,
118: false, args, null);
119: }
120:
121: if (extField.isSaveInDB()) {
122: // save in DB
123: int newMediaID = MediaManager.storeMedia(tmpFile,
124: filename);
125: params.put(target + "."
126: + TKUploadField.NEW_MEDIA_ID, String
127: .valueOf(newMediaID));
128: tmpFile.delete();
129: } else {
130: // Upload kommt ins Filesystem
131: extField
132: .saveToFilesystem(params, tmpFile, filename);
133: t.set(TKUploadField.DOCUMENTS_FROM_FILESYSTEM,
134: Boolean.TRUE);
135: params.remove(target + ".MEDIA_ID"); // falls sie vorher (Umschalten im Formular) da war
136:
137: }
138:
139: // delete the tempfile
140: fileInputStr.close();
141:
142: Object realData = field.compileData("", evt.getParams()
143: .getClass(PARAMETER), new TKHashtable());
144:
145: field.fillIntoTemplate(t, realData, "");
146:
147: extField.extModify(action, t, params, target);
148:
149: } else if (action != null && action.equals(DELETE)) {
150: t = evt.getPrepHTMLTemplate("ce_fields.tmpl");
151:
152: params.put(target + "."
153: + TKUploadField.MEDIA_ID_PARNAME, "");
154: params.put(target, ""); // Filneame
155:
156: Object realData = field.compileData("", evt.getParams()
157: .getClass(PARAMETER), new TKHashtable());
158:
159: field.fillIntoTemplate(t, realData, "");
160:
161: } else {
162: // for actions: CANCEL, MODIFY,
163: t = evt.getPrepHTMLTemplate("ce_upload.tmpl");
164: extField.extModify(action, t, params, target);
165: }
166: CEUtils.keepCEContext(evt, t);
167:
168: WebManEvent.fillEventsIntoTemplate(evt.getRemoteUser(), t,
169: ContextConstants.CONTENT_EDIT,
170: Policy.CONTENT_TREE_ID, ceContext.conNodeId);
171:
172: evt.finishTemplate(t);
173:
174: } catch (Throwable e) {
175: // TO DO : Analyze Exception !
176: throw WebmanExceptionHandler.getException(e);
177: }
178: }
179:
180: /**
181: * Checks wether this handler is capable to handle the specified event.
182: *
183: * @param evt the event to be handled.
184: * @return <CODE>true</CODE> if this handler is capable to handle the
185: * specified event, otherwise <CODE>false</CODE>.
186: */
187: public boolean isHandler(TKEvent evt) {
188: return evt.getName().equalsIgnoreCase(CE_EXT_MODIFY);
189: }
190:
191: }
|