001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.acting;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.parameters.Parameters;
023: import org.apache.avalon.framework.service.ServiceException;
024: import org.apache.avalon.framework.thread.ThreadSafe;
025: import org.apache.cocoon.ProcessingException;
026: import org.apache.cocoon.acting.ServiceableAction;
027: import org.apache.cocoon.environment.ObjectModelHelper;
028: import org.apache.cocoon.environment.Redirector;
029: import org.apache.cocoon.environment.SourceResolver;
030: import org.apache.cocoon.portal.Constants;
031: import org.apache.cocoon.portal.PortalService;
032: import org.apache.cocoon.portal.event.Event;
033: import org.apache.cocoon.portal.event.EventManager;
034: import org.apache.cocoon.portal.event.impl.CopletJXPathEvent;
035:
036: /**
037: * Using this action, you can set values in a coplet
038: *
039: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
040: * @version CVS $Id: CopletSetDataAction.java 526440 2007-04-07 15:28:30Z rgoers $
041: */
042: public class CopletSetDataAction extends ServiceableAction implements
043: ThreadSafe {
044:
045: /* (non-Javadoc)
046: * @see org.apache.cocoon.acting.Action#act(org.apache.cocoon.environment.Redirector, org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
047: */
048: public Map act(Redirector redirector, SourceResolver resolver,
049: Map objectModel, String source, Parameters parameters)
050: throws Exception {
051: PortalService portalService = null;
052: try {
053:
054: portalService = (PortalService) this .manager
055: .lookup(PortalService.ROLE);
056:
057: // determine coplet id
058: String copletId = null;
059: Map context = (Map) objectModel
060: .get(ObjectModelHelper.PARENT_CONTEXT);
061: if (context != null) {
062: copletId = (String) context
063: .get(Constants.COPLET_ID_KEY);
064: } else {
065: copletId = (String) objectModel
066: .get(Constants.COPLET_ID_KEY);
067: }
068:
069: if (copletId == null) {
070: throw new ConfigurationException(
071: "copletId must be passed in the object model either directly (e.g. by using ObjectModelAction) or within the parent context.");
072: }
073:
074: // now traverse parameters:
075: // parameter name is path
076: // parameter value is value
077: // if the value is null or empty, the value is not set!
078: final String[] names = parameters.getNames();
079: if (names != null) {
080: final EventManager publisher = portalService
081: .getComponentManager().getEventManager();
082: for (int i = 0; i < names.length; i++) {
083: final String path = names[i];
084: final String value = parameters.getParameter(path,
085: null);
086: if (value != null && value.trim().length() > 0) {
087: final Event event = new CopletJXPathEvent(
088: portalService
089: .getComponentManager()
090: .getProfileManager()
091: .getCopletInstanceData(copletId),
092: path, value);
093: publisher.send(event);
094: }
095: }
096: }
097:
098: return EMPTY_MAP;
099:
100: } catch (ServiceException e) {
101: throw new ProcessingException(
102: "Unable to lookup component.", e);
103: } finally {
104: this.manager.release(portalService);
105: }
106: }
107: }
|