001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.faces.beans;
034:
035: import com.flexive.faces.FxJsfUtils;
036: import com.flexive.faces.messages.FxFacesMsgErr;
037: import com.flexive.shared.EJBLookup;
038: import com.flexive.shared.content.FxContent;
039: import com.flexive.shared.content.FxData;
040: import com.flexive.shared.content.FxPK;
041: import org.apache.commons.lang.StringUtils;
042:
043: import javax.faces.application.FacesMessage;
044: import javax.faces.context.FacesContext;
045: import java.io.Serializable;
046: import java.util.Map;
047:
048: /**
049: * Provides actions that may be useful for (not only) pages using
050: * the {@link com.flexive.faces.components.toolkit.FxContentView FxContentView} component.
051: *
052: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
053: * @version $Rev: 1 $
054: */
055: public class FxContentViewBean implements Serializable {
056: private static final long serialVersionUID = 3463665467310989039L;
057: /**
058: * Request attribute key where the PK of the saved content will be stored on success.
059: */
060: public static final String REQUEST_PK = FxContentViewBean.class
061: .getName()
062: + ".PK";
063: /**
064: * Request attribute key where the content instance will be stored on success.
065: */
066: public static final String REQUEST_CONTENT = FxContentViewBean.class
067: .getName()
068: + ".CONTENT";
069: /**
070: * Request attribute for a boolean flag that is set to true when a new content
071: * instance was created.
072: */
073: public static final String REQUEST_ISNEW = FxContentViewBean.class
074: .getName()
075: + ".ISNEW";
076: private FxContent content;
077: private String xpath;
078: private String successMessage;
079:
080: /**
081: * Persist the instance stored in <code>fxContentViewBean.content</code> to the DB.
082: *
083: * @return <ul>
084: * <li><code>success</code> if the content could be saved successfully</li>
085: * <li><code>failure</code> if an error occured<li>
086: * </ul>
087: */
088: public String save() {
089: try {
090: final FxPK pk = EJBLookup.getContentEngine().save(
091: content.copy());
092: final Map<String, Object> requestMap = FacesContext
093: .getCurrentInstance().getExternalContext()
094: .getRequestMap();
095: requestMap.put(REQUEST_PK, pk);
096: requestMap.put(REQUEST_CONTENT, content);
097: requestMap
098: .put(REQUEST_ISNEW, content.getPk().getId() == -1);
099: if (StringUtils.isNotBlank(successMessage)) {
100: FxJsfUtils.addMessage(new FacesMessage(successMessage));
101: }
102: return "success";
103: } catch (Exception e) {
104: new FxFacesMsgErr(e).addToContext();
105: return "failure";
106: }
107: }
108:
109: /**
110: * Add an empty element or group for the given XPath.
111: *
112: * @return <ul>
113: * <li><code>success</code> if the element or group was added successfully</li>
114: * <li><code>failure</code> if an error occured<li>
115: * </ul>
116: */
117: public String add() {
118: try {
119: content.getGroupData(xpath).createNew(
120: FxData.POSITION_BOTTOM);
121: return "success";
122: } catch (Exception e) {
123: new FxFacesMsgErr(e).addToContext();
124: return "failure";
125: }
126: }
127:
128: /**
129: * Remove the given XPath from the content instance.
130: *
131: * @return <ul>
132: * <li><code>success</code> if the group or element was removed successfully</li>
133: * <li><code>failure</code> if an error occured<li>
134: * </ul>
135: */
136: public String remove() {
137: try {
138: content.remove(xpath);
139: return "success";
140: } catch (Exception e) {
141: new FxFacesMsgErr(e).addToContext();
142: return "failure";
143: }
144: }
145:
146: public FxContent getContent() {
147: return content;
148: }
149:
150: public void setContent(FxContent content) {
151: this .content = content;
152: }
153:
154: public String getXpath() {
155: return xpath;
156: }
157:
158: public void setXpath(String xpath) {
159: this .xpath = xpath;
160: }
161:
162: public String getSuccessMessage() {
163: return successMessage;
164: }
165:
166: public void setSuccessMessage(String successMessage) {
167: this.successMessage = successMessage;
168: }
169: }
|