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.ItemType;
38  import info.magnolia.cms.core.Path;
39  import info.magnolia.cms.security.AccessDeniedException;
40  import info.magnolia.jcr.util.SessionUtil;
41  import info.magnolia.module.admininterface.UUIDSaveHandler;
42  import info.magnolia.module.data.DataConsts;
43  
44  import java.util.Iterator;
45  
46  import javax.jcr.Node;
47  import javax.jcr.PathNotFoundException;
48  import javax.jcr.RepositoryException;
49  
50  import org.apache.commons.lang.StringUtils;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  import net.sf.json.JSONArray;
55  import net.sf.json.JSONObject;
56  
57  /**
58   * Save handler taking care of saving controls implementing MultiValueControl interface.
59   *
60   * @author had
61   */
62  public class MultiControlSaveHandler extends UUIDSaveHandler {
63  
64      private static final Logger log = LoggerFactory.getLogger(MultiControlSaveHandler.class);
65      private static final String IMAGE_PROPERTY = "img";
66  
67      @Override
68      protected void processString(Content node, String name, int type,
69                                   int encoding, String[] values, String valueStr)
70              throws PathNotFoundException, RepositoryException,
71              AccessDeniedException {
72          Object control = this.getDialog().getSub(name);
73          // only handle special multi selects
74          if (control instanceof MultiValueControl) {
75              try {
76                  // delete existing content
77                  node.delete(name);
78              } catch (PathNotFoundException e) {
79                  // node does not exist, yet (ok)
80              }
81              Content filters = node.createContent(name, new ItemType(DataConsts.MODULE_DATA_CONTENT_NODE_TYPE));
82              new JSONArray();
83              JSONArray json = JSONArray.fromObject(valueStr);
84              for (int i = 0; i < json.size(); i++) {
85                  Content content = filters.createContent(Path.getUniqueLabel(filters, "00"), new ItemType(DataConsts.MODULE_DATA_CONTENT_NODE_TYPE));
86                  JSONObject value = json.getJSONObject(i);
87                  Iterator<String> iter = value.keys();
88                  while (iter.hasNext()) {
89                      String key = iter.next();
90  
91                      if (StringUtils.equals(IMAGE_PROPERTY, key)) {
92                          try {
93                              if (value.get(key) != null && StringUtils.isNotBlank(value.get(key).toString())) {
94                                  Node fn = SessionUtil.getNode("dms", value.get(key).toString());
95                                  if (fn != null) {
96                                      content.createNodeData("imgUUID", fn.getIdentifier());
97                                  }
98                              }
99                          } catch (Exception e) {
100                             log.error("Could not get UUID for image stored in DMS: " + e.getMessage());
101                         }
102                     }
103                     content.createNodeData(key, value.get(key));
104                 }
105             }
106         } else {
107             super.processString(node, name, type, encoding, values, valueStr);
108         }
109     }
110 }