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.war.beans.admin.tree;
034:
035: import com.flexive.faces.FxJsfUtils;
036: import com.flexive.faces.beans.ActionBean;
037: import com.flexive.faces.messages.FxFacesMsgErr;
038: import com.flexive.shared.CacheAdmin;
039: import com.flexive.shared.EJBLookup;
040: import com.flexive.shared.content.FxPK;
041: import com.flexive.shared.exceptions.FxApplicationException;
042: import com.flexive.shared.interfaces.TemplateEngine;
043: import com.flexive.shared.interfaces.TreeEngine;
044: import com.flexive.shared.structure.FxType;
045: import com.flexive.shared.tree.FxTemplateInfo;
046: import com.flexive.shared.tree.FxTemplateMapping;
047: import com.flexive.shared.tree.FxTreeMode;
048: import static com.flexive.shared.tree.FxTreeMode.Edit;
049: import org.apache.commons.lang.StringUtils;
050: import org.apache.commons.logging.Log;
051: import org.apache.commons.logging.LogFactory;
052:
053: import javax.faces.event.ActionEvent;
054: import javax.faces.model.SelectItem;
055: import java.util.ArrayList;
056: import java.util.List;
057:
058: public class TreeNodeEditorBean implements ActionBean {
059: private static final Log LOG = LogFactory
060: .getLog(TreeNodeEditorBean.class);
061:
062: private long nodeId;
063: private TemplateEngine templateEngine;
064: private TreeEngine tree;
065: public final static String PAGE_EDITNODE = "treeNodeEditor";
066: private long contentType;
067: private FxTemplateInfo template;
068: private ArrayList<FxExtendedTemplateMapping> mappings;
069: private List<FxType> unmappedTypes;
070: private Long contentTypeId;
071: private FxPK contentPk;
072:
073: public static class FxExtendedTemplateMapping extends
074: FxTemplateMapping {
075: private String contentTypeName;
076: private String templateName;
077:
078: // TODO: What if template is NOT Live, but content goes live? warning?
079: public FxExtendedTemplateMapping(Long contentType,
080: long templateId, TemplateEngine te)
081: throws FxApplicationException {
082: super (contentType, templateId);
083: this .contentTypeName = contentType == null ? "*"
084: : CacheAdmin.getEnvironment().getType(contentType)
085: .getDisplayName();
086: this .templateName = te.getInfo(templateId, FxTreeMode.Edit)
087: .getName();
088: }
089:
090: public FxExtendedTemplateMapping(FxTemplateMapping m,
091: TemplateEngine te) throws FxApplicationException {
092: super (m.getContentType(), m.getTemplateId());
093: this .contentTypeName = m.getContentType() == null ? "*"
094: : CacheAdmin.getEnvironment().getType(
095: m.getContentType()).getDisplayName();
096: this .templateName = te.getInfo(m.getTemplateId(),
097: FxTreeMode.Edit).getName();
098: }
099:
100: public String getContentTypeName() {
101: return contentTypeName;
102: }
103:
104: public String getTemplateName() {
105: return templateName;
106: }
107: }
108:
109: public String getParseRequestParameters() {
110: try {
111: String action = FxJsfUtils.getParameter("action");
112: if (StringUtils.isBlank(action)) {
113: // no action requested
114: return null;
115: }
116: if ("editNode".equals(action)) {
117: setNodeId(FxJsfUtils.getLongParameter("id"));
118: load();
119: }
120: // hack!
121: FxJsfUtils.resetFaceletsComponent("frm");
122: } catch (Throwable t) {
123: // TODO possibly pass some error message to the HTML page
124: LOG.error("Failed to parse request parameters: "
125: + t.getMessage(), t);
126: }
127: return null;
128: }
129:
130: public FxTemplateInfo getTemplate() {
131: return template;
132: }
133:
134: public void setTemplate(FxTemplateInfo data) {
135: this .template = data;
136: }
137:
138: public long getContentType() {
139: return contentType;
140: }
141:
142: public void setContentType(long contentType) {
143: this .contentType = contentType;
144: }
145:
146: public Long getContentTypeId() {
147: return contentTypeId;
148: }
149:
150: public void setContentTypeId(Long contenTypeId) {
151: this .contentTypeId = contenTypeId;
152: }
153:
154: public void addTemplateMapping(ActionEvent event) {
155: try {
156: FxExtendedTemplateMapping mp = new FxExtendedTemplateMapping(
157: contentType != -1 ? contentType : null, template
158: .getId(), templateEngine);
159: mappings.add(mp);
160: } catch (Throwable t) {
161: new FxFacesMsgErr(t).addToContext();
162: } finally {
163: computeSettings();
164: FxJsfUtils.resetFaceletsComponent("frm:mappings");
165: }
166: }
167:
168: public void removeTemplateMapping(ActionEvent event) {
169: try {
170: if (this .mappings == null)
171: return;
172: for (FxExtendedTemplateMapping m : mappings) {
173: if (m.getContentType() == null && contentTypeId != null)
174: continue;
175: if ((m.getContentType() == null && contentTypeId == null)
176: || m.getContentType().equals(contentTypeId)) {
177: mappings.remove(m);
178: break;
179: }
180: }
181: } catch (Throwable t) {
182: new FxFacesMsgErr(t).addToContext();
183: } finally {
184: computeSettings();
185: FxJsfUtils.resetFaceletsComponent("frm:mappings");
186: }
187: }
188:
189: public List<SelectItem> getUnmappedTypes() {
190: if (hasMappingForType(null)) {
191: return FxJsfUtils.asSelectListWithLabel(unmappedTypes,
192: false);
193: } else {
194: return FxJsfUtils
195: .asSelectListWithLabel(unmappedTypes, true);
196: }
197: }
198:
199: public boolean getHasUnmappedTypes() {
200: return unmappedTypes != null && unmappedTypes.size() > 0;
201: }
202:
203: public void computeSettings() {
204: List<FxType> types = CacheAdmin.getFilteredEnvironment()
205: .getTypes(true, true, true, false);
206: // Fill unmapped types
207: if (unmappedTypes != null) {
208: unmappedTypes.clear();
209: } else {
210: unmappedTypes = new ArrayList<FxType>(types.size());
211: }
212: for (FxType t : types) {
213: if (!t.getName().equalsIgnoreCase("ROOT")
214: && !hasMappingForType(t)) {
215: unmappedTypes.add(t);
216: }
217: }
218: // Fill inherited
219: }
220:
221: private boolean hasMappingForType(FxType type) {
222: // General (all types) mapping
223: if (type == null) {
224: for (FxTemplateMapping m : mappings) {
225: if (m.getContentType() == null)
226: return true;
227: }
228: return false;
229: }
230: // All other mappings
231: if (mappings == null)
232: return false;
233: for (FxTemplateMapping m : mappings) {
234: if (m.getContentType() == null)
235: continue;
236: if (m.getContentType() == type.getId())
237: return true;
238: }
239: return false;
240: }
241:
242: public List<FxExtendedTemplateMapping> getMappings() {
243: return mappings == null ? new ArrayList<FxExtendedTemplateMapping>(
244: 0)
245: : mappings;
246: }
247:
248: public void setMappings(
249: ArrayList<FxExtendedTemplateMapping> mappings) {
250: this .mappings = mappings;
251: computeSettings();
252: }
253:
254: /**
255: * Constructor.
256: * <p/>
257: * <p/>
258: * Initializes the EJB interfaces
259: */
260: public TreeNodeEditorBean() {
261: try {
262: tree = EJBLookup.getTreeEngine();
263: templateEngine = EJBLookup.getTemplateEngine();
264: } catch (Throwable t) {
265: new FxFacesMsgErr(t).addToContext();
266: }
267: }
268:
269: /**
270: * Initializes the beans using the value of the nodeId variable.
271: *
272: * @return the next page to render (always the node editor)
273: */
274: public String load() {
275: this .mappings = null;
276: this .contentTypeId = null;
277: this .contentType = -1;
278: this .unmappedTypes = null;
279: this .contentPk = null;
280: try {
281: List<FxTemplateMapping> mp = templateEngine
282: .getTemplateMappings(nodeId, FxTreeMode.Edit);
283: this .mappings = new ArrayList<FxExtendedTemplateMapping>(mp
284: .size());
285: for (FxTemplateMapping m : mp) {
286: this .mappings.add(new FxExtendedTemplateMapping(m,
287: templateEngine));
288: }
289: try {
290: this .contentPk = tree.getNode(Edit, nodeId)
291: .getReference();
292: } catch (Throwable t) {
293: // ignore
294: }
295: } catch (Throwable t) {
296: new FxFacesMsgErr(t).addToContext();
297: } finally {
298: computeSettings();
299: }
300: return PAGE_EDITNODE;
301: }
302:
303: public long getNodeId() {
304: return nodeId;
305: }
306:
307: public void setNodeId(long nodeId) {
308: this .nodeId = nodeId;
309: }
310:
311: public String save() {
312: try {
313: ArrayList<FxTemplateMapping> mp = new ArrayList<FxTemplateMapping>(
314: mappings == null ? 0 : mappings.size());
315: if (mappings != null) {
316: for (FxExtendedTemplateMapping m : mappings) {
317: mp.add(new FxTemplateMapping(m.getContentType(), m
318: .getTemplateId()));
319: }
320: templateEngine.setTemplateMappings(nodeId, mp);
321: }
322: } catch (Throwable t) {
323: new FxFacesMsgErr(t).addToContext();
324: }
325: return load();
326: }
327:
328: public String gotoContentEditor() {
329: return "contentEditor";
330: }
331: }
|