View Javadoc

1   /**
2    * This file Copyright (c) 2008-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.rssaggregator;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.NodeData;
38  import info.magnolia.cms.gui.control.Button;
39  import info.magnolia.cms.gui.control.ControlImpl;
40  import info.magnolia.cms.gui.dialog.DialogBox;
41  import info.magnolia.freemarker.FreemarkerUtil;
42  
43  import java.io.IOException;
44  import java.io.PrintWriter;
45  import java.io.Writer;
46  import java.util.ArrayList;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  import javax.jcr.PathNotFoundException;
51  import javax.jcr.RepositoryException;
52  
53  import org.apache.commons.lang.BooleanUtils;
54  import org.apache.commons.lang.StringUtils;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  /**
59   * Common class for creating combined multi controls.
60   * @author had
61   *
62   */
63  public abstract class MultiControl extends DialogBox implements MultiValueControl {
64      private static final Logger log = LoggerFactory.getLogger(MultiControl.class);
65  
66      /**
67       * Create the object to initialize the table.
68       */
69      public String getJSON() {
70          final List<String> values = this.getValues();
71  
72          if (values.size() == 0) {
73              return getEmpty();
74          }
75  
76          // enclose each value with {}
77          final List<String> objects = new ArrayList<String>();
78          for (Iterator<String> iter = values.iterator(); iter.hasNext();) {
79              final String value = iter.next();
80              log.debug("value:" + value);
81              objects.add("{" + value + "}");
82          }
83          return "[" + StringUtils.join(objects.iterator(), ",") + "]";
84      }
85  
86      protected abstract String getEmpty();
87  
88      protected List<String> readValues() {
89          List<String> values = new ArrayList<String>();
90          if (this.getStorageNode() != null) {
91              try {
92                  Content data = this.getStorageNode().getContent(getName());
93                  Iterator<Content> iter = data.getChildren().iterator();
94                  while (iter.hasNext()) {
95                      Content value = iter.next();
96                      Iterator<NodeData> dataVals = value.getNodeDataCollection().iterator();
97                      StringBuffer buf = new StringBuffer();
98                      while (dataVals.hasNext()) {
99                          NodeData partial = dataVals.next();
100                         if (buf.length() > 0) {
101                             buf.append(",");
102                         }
103                             buf.append(partial.getName()).append(":'").append(partial.getString()).append("'");
104                     }
105                     values.add(buf.toString());
106                 }
107             } catch (PathNotFoundException e) {
108                 // not yet existing: OK
109             }
110             catch (RepositoryException re) {
111                 log.error("can't set values", re);
112             }
113         }
114         return values;
115     }
116 
117     public void drawHtml(Writer w) throws IOException {
118         PrintWriter out = (PrintWriter) w;
119         this.drawHtmlPre(out);
120         out.write(FreemarkerUtil.process(MultiControl.class, this));
121         this.drawHtmlPost(out);
122 
123     }
124 
125     public String getSaveInfo() {
126         Boolean renderSaveInfo = BooleanUtils.toBooleanObject(this.getConfigValue("saveInfo"));
127         if (BooleanUtils.toBooleanDefaultIfNull(renderSaveInfo, true)) {
128             ControlImpl dummy = new ControlImpl(this.getName(), (String) null);
129             //dummy.setValueType(ControlImpl.VALUETYPE_MULTIPLE);
130             return dummy.getHtmlSaveInfo();
131         }
132         // don' create the save info
133         return "";
134     }
135 
136     /**
137      * The button to add a new row.
138      */
139     public String getAddButton() {
140         Button add = new Button();
141         add.setLabel(getMessage("buttons.add"));
142         add.setOnclick(this.getName() + "DynamicTable.addNew();");
143         add.setSmall(true);
144         return add.getHtml();
145     }
146 
147     /**
148      * Button for deleting a row.
149      */
150     public String getDeleteButton() {
151         Button delete = new Button();
152         delete.setLabel(this.getMessage("buttons.delete"));
153         delete.setOnclick(this.getName() + "DynamicTable.del('${index}');" + this.getName() + "DynamicTable.persist();");
154         delete.setSmall(true);
155         return delete.getHtml();
156     }
157 
158     /**
159      * If the values are saved using the valueType multiple, we can not use the same name for the hidden field we use
160      * for persisting the data.
161      * @return the name of the hidden field
162      */
163     public String getHiddenFieldName() {
164         return this.getName();
165     }
166 
167     /**
168      * JS function used to create an object out of the input fields.
169      */
170     public String getGetObjectFunction() {
171         return "function(prefix, index){" +
172         " var obj = new Object();" +
173         " obj.link = document.getElementById(prefix + '_link').value;" +
174         " obj.title = document.getElementById(prefix + '_title').value; " +
175         " return obj;" +
176         "}";
177     }
178 
179     /**
180      * JS function used to create a new empty object.
181      */
182     public String getNewObjectFunction() {
183         return "function(){" +
184         " var obj = new Object();" +
185         " obj.link = ''; " +
186         " obj.title = ''; " +
187         " return obj;" +
188         "}";
189     }
190 }