001: /*
002: * Copyright 2005 jWic group (http://www.jwic.de)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * de.jwic.base.DefaultActionController
017: * Created on 30.10.2004
018: * $Id: DefaultActionController.java,v 1.2 2006/03/14 13:05:44 lordsam Exp $
019: */
020: package de.jwic.base;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import de.jwic.upload.UploadFile;
026:
027: /**
028: * Default implementation of the IActionController interface.
029: * @author Florian Lippisch
030: * @version $Revision: 1.2 $
031: */
032: public class DefaultActionController implements IActionController {
033:
034: protected final Log log = LogFactory.getLog(getClass());
035:
036: /* (non-Javadoc)
037: * @see de.jwic.base.IActionController#handleField(de.jwic.base.SessionContext, java.lang.String, java.lang.String)
038: */
039: public void handleField(SessionContext sc, ValueChangedQueue queue,
040: String fieldname, String[] values) {
041:
042: String[] ctrlIds = getControlIdFromField(fieldname);
043: if (ctrlIds != null) {
044: Control ctrl = sc.getControlById(ctrlIds[0]);
045: Field field = ctrl.getField(ctrlIds[1]);
046: if (field != null) {
047: if (log.isDebugEnabled()) {
048: StringBuffer sb = new StringBuffer();
049: for (int i = 0; i < values.length; i++) {
050: if (i > 0) {
051: sb.append(";");
052: }
053: sb.append(values[i]);
054: }
055: log.debug("Saving (" + values.length + ") values '"
056: + sb + "' to field '" + field.getName()
057: + "' in control " + ctrlIds[0]);
058: }
059: field.batchUpdate(values, queue);
060: } else {
061: log.warn("The field with the name '" + ctrlIds[1]
062: + "' does not exist in the control '"
063: + ctrlIds[0] + "'");
064: // XXX Remove in later versions (FLI 28.08.2005)
065: field = new Field(ctrl, ctrlIds[1]);
066: field.batchUpdate(values, queue);
067: }
068: }
069:
070: }
071:
072: /* (non-Javadoc)
073: * @see de.jwic.base.IActionController#handleFile(de.jwic.base.SessionContext, java.lang.String, de.jwic.upload.UploadFile)
074: */
075: public void handleFile(SessionContext sc, String fieldname,
076: UploadFile file) {
077:
078: String[] ctrlIds = getControlIdFromField(fieldname);
079: if (ctrlIds != null) {
080: Control ctrl = sc.getControlById(ctrlIds[0]);
081: if (ctrl instanceof IFileReciever) {
082: IFileReciever reciever = (IFileReciever) ctrl;
083: reciever.handleFile(ctrlIds[1], file);
084: } else {
085: log
086: .error("Can not store file in "
087: + ctrl.getControlID()
088: + " because the control does not implement IFileReciever interface.");
089: }
090: }
091:
092: }
093:
094: /* (non-Javadoc)
095: * @see de.jwic.base.IActionController#handleAction(de.jwic.base.SessionContext, java.lang.String, java.lang.String)
096: */
097: public void handleAction(SessionContext sc, String ctrlId,
098: String action, String param)
099: throws ControlNotFoundException {
100:
101: // if the session had been removed ctrlId, action and param is ""
102: if (ctrlId != null && ctrlId.length() > 0) {
103: Control ctrl = sc.getControlById(ctrlId);
104: ctrl.actionPerformed(action, param);
105: }
106: }
107:
108: /**
109: * @param paramName
110: * @return
111: */
112: private String[] getControlIdFromField(String paramName) {
113:
114: if (paramName.startsWith("fld_")) {
115: String fieldId = paramName.substring(4);
116: int lastDot = fieldId.lastIndexOf('.');
117: if (lastDot != -1) {
118: String ctrlId = fieldId.substring(0, lastDot);
119: String fieldname = fieldId.substring(lastDot + 1);
120: return new String[] { ctrlId, fieldname };
121: }
122: }
123: return null;
124: }
125:
126: }
|