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.javascript.tree;
034:
035: import com.flexive.shared.EJBLookup;
036: import com.flexive.shared.FxContext;
037: import com.flexive.shared.content.FxPK;
038: import com.flexive.shared.interfaces.TreeEngine;
039: import com.flexive.shared.tree.FxTreeMode;
040: import static com.flexive.shared.tree.FxTreeMode.Edit;
041: import static com.flexive.shared.tree.FxTreeMode.Live;
042: import com.flexive.shared.tree.FxTreeNode;
043: import com.flexive.shared.tree.FxTreeNodeEdit;
044: import com.flexive.war.JsonWriter;
045: import org.apache.commons.lang.StringUtils;
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048:
049: import java.io.IOException;
050: import java.io.Serializable;
051: import java.io.StringWriter;
052:
053: /**
054: * Content tree edit actions invoked via JSON/RPC.
055: *
056: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
057: * @version $Rev: 1 $
058: */
059: public class ContentTreeEditor implements Serializable {
060: private static final long serialVersionUID = -6518286246610051456L;
061: private static final Log LOG = LogFactory
062: .getLog(ContentTreeEditor.class);
063:
064: /**
065: * Update the tree node's label.
066: *
067: * @param nodeId the node id
068: * @param label the new label
069: * @param liveTree true if the live tree should be rendered, false for the edit tree
070: * @param pathMode true if node labels should be paths instead of content captions
071: * @return nothing
072: * @throws java.io.IOException if the response could not be created
073: */
074: public String saveLabel(long nodeId, String label,
075: boolean liveTree, boolean pathMode) throws IOException {
076:
077: // Rename the node
078: try {
079: if (label.endsWith("<br>")) {
080: label = label.substring(0, label.length() - 4);
081: }
082: final FxTreeNodeEdit node = EJBLookup.getTreeEngine()
083: .getNode(liveTree ? Live : Edit, nodeId)
084: .asEditable();
085: if (pathMode) {
086: node.setName(label);
087: } else {
088: node.getLabel().setTranslation(
089: FxContext.get().getTicket().getLanguage(),
090: label);
091: }
092: EJBLookup.getTreeEngine().save(node);
093: // EJBLookup.getTreeInterface().renameNode(nodeId,false,label,null);
094: } catch (Exception e) {
095: LOG.error("Failed to save label: " + e.getMessage(), e);
096: }
097:
098: String title = StringUtils.defaultString(label).trim();
099: try {
100: // add subnode count
101: final FxTreeNode node = EJBLookup.getTreeEngine().getNode(
102: Edit, nodeId);
103: int totalChildren = node.getTotalChildCount();
104: if (totalChildren > 0) {
105: title += " [" + totalChildren + "]";
106: }
107:
108: // add dirty flag
109: if (node.isDirty()) {
110: title = "<span class=\"dirty\">" + title + "</span>";
111: }
112: } catch (Exception e) {
113: LOG.error("Failed to save label: " + e.getMessage(), e);
114: }
115:
116: // response: [{title: 'new title'}]
117: StringWriter out = new StringWriter();
118: new JsonWriter(out).startArray().startMap().writeAttribute(
119: "title", title).closeMap().closeArray()
120: .finishResponse();
121: return out.toString();
122: }
123:
124: /**
125: * Move the given node to the new parent at position <code>index</code>
126: *
127: * @param nodeId the tree node to be moved
128: * @param newParentId the the parent node
129: * @param index the new position
130: * @return nothing
131: * @throws Exception if an error occured
132: */
133: public String move(long nodeId, long newParentId, int index,
134: boolean live) throws Exception {
135: try {
136: EJBLookup.getTreeEngine().move(live ? Live : Edit, nodeId,
137: newParentId, index);
138: } catch (Exception e) {
139: LOG.error("Failed to move node: " + e, e);
140: throw e;
141: }
142: return "[]";
143: }
144:
145: /**
146: * Remove a tree node and (depending on the removeContent parameter) the attached
147: * content instance.
148: *
149: * @param nodeId the tree node ID
150: * @param removeContent if true, the attached content instance will also be removed
151: * @param live if the tree is in live mode
152: * @param deleteChildren if the node children should also be deleted
153: * @return nothing
154: * @throws Exception if an error occured
155: */
156: public String remove(long nodeId, boolean removeContent,
157: boolean live, boolean deleteChildren) throws Exception {
158: try {
159: FxTreeNode node = EJBLookup.getTreeEngine().getNode(
160: live ? Live : Edit, nodeId);
161: EJBLookup.getTreeEngine().remove(node, removeContent,
162: deleteChildren);
163: } catch (Exception e) {
164: LOG.error("Failed to delete node: " + e, e);
165: throw e;
166: }
167: return "[]";
168: }
169:
170: public String addReferences(long nodeId, long[] referenceIds)
171: throws Exception {
172: try {
173: TreeEngine tree = EJBLookup.getTreeEngine();
174: for (long referenceId : referenceIds) {
175: tree.save(FxTreeNodeEdit.createNew(
176: String.valueOf(referenceId)).setParentNodeId(
177: nodeId).setReference(new FxPK(referenceId)));
178: // tree.createNode(nodeId, String.valueOf(referenceId), referenceId);
179: }
180: } catch (Exception e) {
181: LOG.error("Failed to add nodes: " + e.getMessage(), e);
182: throw e;
183: }
184: return "[]";
185: }
186:
187: public String createFolder(long parentNodeId, String folderName)
188: throws Exception {
189: try {
190: EJBLookup.getTreeEngine().save(
191: FxTreeNodeEdit.createNew(folderName)
192: .setParentNodeId(parentNodeId));
193: } catch (Exception e) {
194: LOG.error(
195: "Failed to create tree folder: " + e.getMessage(),
196: e);
197: throw e;
198: }
199: return "[]";
200: }
201:
202: public String activate(long nodeId, boolean includeChildren)
203: throws Exception {
204: try {
205: EJBLookup.getTreeEngine().activate(FxTreeMode.Edit, nodeId,
206: includeChildren);
207: } catch (Exception e) {
208: LOG.error("Failed to activate node: " + e.getMessage(), e);
209: throw e;
210: }
211: return "[]";
212: }
213:
214: public String copy(long nodeId, long newParentId, int index,
215: boolean live) throws Exception {
216: try {
217: EJBLookup.getTreeEngine().copy(live ? Live : Edit, nodeId,
218: newParentId, index);
219: } catch (Exception e) {
220: LOG.error("Failed to copy node: " + e.getMessage(), e);
221: throw e;
222: }
223: return "[]";
224: }
225: }
|