View Javadoc

1   /**
2    * This file Copyright (c) 2008-2013 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      @Override
89      protected List<String> readValues() {
90          List<String> values = new ArrayList<String>();
91          if (this.getStorageNode() != null) {
92              try {
93                  Content data = this.getStorageNode().getContent(getName());
94                  Iterator<Content> iter = data.getChildren().iterator();
95                  while (iter.hasNext()) {
96                      Content value = iter.next();
97                      Iterator<NodeData> dataVals = value.getNodeDataCollection().iterator();
98                      StringBuffer buf = new StringBuffer();
99                      while (dataVals.hasNext()) {
100                         NodeData partial = dataVals.next();
101                         if (buf.length() > 0) {
102                             buf.append(",");
103                         }
104                             buf.append(partial.getName()).append(":'").append(partial.getString()).append("'");
105                     }
106                     values.add(buf.toString());
107                 }
108             } catch (PathNotFoundException e) {
109                 // not yet existing: OK
110             }
111             catch (RepositoryException re) {
112                 log.error("can't set values", re);
113             }
114         }
115         return values;
116     }
117 
118     @Override
119     public void drawHtml(Writer w) throws IOException {
120         PrintWriter out = (PrintWriter) w;
121         this.drawHtmlPre(out);
122         out.write(FreemarkerUtil.process(MultiControl.class, this));
123         this.drawHtmlPost(out);
124 
125     }
126 
127     public String getSaveInfo() {
128         Boolean renderSaveInfo = BooleanUtils.toBooleanObject(this.getConfigValue("saveInfo"));
129         if (BooleanUtils.toBooleanDefaultIfNull(renderSaveInfo, true)) {
130             ControlImpl dummy = new ControlImpl(this.getName(), (String) null);
131             //dummy.setValueType(ControlImpl.VALUETYPE_MULTIPLE);
132             return dummy.getHtmlSaveInfo();
133         }
134         // don' create the save info
135         return "";
136     }
137 
138     /**
139      * The button to add a new row.
140      */
141     public String getAddButton() {
142         Button add = new Button();
143         add.setLabel(getMessage("buttons.add"));
144         add.setOnclick(this.getName() + "DynamicTable.addNew();");
145         add.setSmall(true);
146         return add.getHtml();
147     }
148 
149     /**
150      * Button for deleting a row.
151      */
152     public String getDeleteButton() {
153         Button delete = new Button();
154         delete.setLabel(this.getMessage("buttons.delete"));
155         delete.setOnclick(this.getName() + "DynamicTable.del('${index}');" + this.getName() + "DynamicTable.persist();");
156         delete.setSmall(true);
157         return delete.getHtml();
158     }
159 
160     /**
161      * If the values are saved using the valueType multiple, we can not use the same name for the hidden field we use
162      * for persisting the data.
163      * @return the name of the hidden field
164      */
165     public String getHiddenFieldName() {
166         return this.getName();
167     }
168 
169     /**
170      * JS function used to create an object out of the input fields.
171      */
172     public String getGetObjectFunction() {
173         return "function(prefix, index){" +
174         " var obj = new Object();" +
175         " obj.link = document.getElementById(prefix + '_link').value;" +
176         " obj.title = document.getElementById(prefix + '_title').value; " +
177         " return obj;" +
178         "}";
179     }
180 
181     /**
182      * JS function used to create a new empty object.
183      */
184     public String getNewObjectFunction() {
185         return "function(){" +
186         " var obj = new Object();" +
187         " obj.link = ''; " +
188         " obj.title = ''; " +
189         " return obj;" +
190         "}";
191     }
192 }