View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 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.templatingkit.templates;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.ItemType;
38  import info.magnolia.cms.security.AccessDeniedException;
39  import info.magnolia.cms.util.ContentUtil;
40  import info.magnolia.module.templatingkit.functions.STKTemplatingFunctions;
41  import info.magnolia.module.templatingkit.templates.pages.STKPage;
42  import info.magnolia.module.templatingkit.util.STKUtil;
43  import info.magnolia.rendering.model.RenderingModel;
44  import info.magnolia.templating.functions.TemplatingFunctions;
45  
46  import java.util.List;
47  import java.util.Map;
48  
49  import javax.inject.Inject;
50  import javax.jcr.Node;
51  import javax.jcr.PathNotFoundException;
52  import javax.jcr.RepositoryException;
53  
54  /**
55   * Auto generation of Components is provided by the {@link info.magnolia.rendering.template.TemplateDefinition}.
56   * @author cringele
57   * @deprecated since STK 2.0: Auto generation of Components is provided by the {@link info.magnolia.rendering.template.TemplateDefinition}.
58   *
59   */
60  public class SingletonParagraphTemplateModel extends STKTemplateModel<STKPage> {
61  
62      private static final String SINGLETON_NODE_NAME = "singleton";
63  
64      @Inject
65      public SingletonParagraphTemplateModel(Node content, STKPage definition, RenderingModel<?> parent, STKTemplatingFunctions stkFunctions, TemplatingFunctions templatingFunctions) {
66          super(content, definition, parent, stkFunctions, templatingFunctions);
67      }
68  
69      @Override
70      public String execute() {
71          final STKPage templateDef = this.getDefinition();
72          try {
73              STKUtil.doPrivileged(ContentUtil.asContent(content), new STKUtil.PrivilegedOperation(){
74                  @Override
75                  public void exec(Content contentInSystemContext) throws RepositoryException {
76                      createMainArea(contentInSystemContext, templateDef);
77                      createExtrasArea(contentInSystemContext, templateDef);
78                  }
79              });
80          }
81          catch (RepositoryException e) {
82              throw new RuntimeException("can't auto generate the main or extra area", e);
83          }
84          return super.execute();
85      }
86  
87      private void createExtrasArea(Content contentInSystemContext, STKPage templateDef) {
88  
89          if( templateDef.getArea("extras") != null && templateDef.getArea("extras") instanceof AutoGeneratedExtrasArea) {
90  
91              AutoGeneratedExtrasArea singletonDef = (AutoGeneratedExtrasArea)templateDef.getArea("extras");
92              try {
93                  Content extrasNode = ContentUtil.getOrCreateContent(ContentUtil.asContent(content), "extras", ItemType.CONTENTNODE, true);
94                  //for now: render all in first column
95                  Content extrasColumn = ContentUtil.getOrCreateContent(extrasNode, "extras1" , ItemType.CONTENTNODE, true);
96                  createExtrasColumnItems(singletonDef, extrasColumn);
97                  extrasColumn.save();
98  
99              } catch (Exception e) {
100                 throw new IllegalStateException("Can't create extras paragraph", e);
101             }
102         }
103 
104     }
105 
106     private void createExtrasColumnItems(AutoGeneratedExtrasArea singletonDef, Content extrasColumn)
107             throws RepositoryException, PathNotFoundException, AccessDeniedException {
108         List paragraphs = singletonDef.getAutoGeneratedParagraphs();
109         for(int i = 0; i < paragraphs.size(); i++ ){
110             if (!extrasColumn.hasContent("extrasItem"+ i)) {
111                 Content singletonNode = extrasColumn.createContent("extrasItem" + i, ItemType.CONTENTNODE);
112                 singletonNode.getMetaData().setTemplate(((AutoGeneratedParagraph)paragraphs.get(i)).getName());
113 
114                 setDefaultValues(singletonNode, ((AutoGeneratedParagraph)paragraphs.get(i)).getDefaultValues());
115             }
116         }
117     }
118 
119     private void createMainArea(Content contentInSystemContext, STKPage templateDef) {
120         SingletonParagraphMainArea singletonDef = (SingletonParagraphMainArea)templateDef.getArea("main");
121         try {
122             Content mainCollection = ContentUtil.getOrCreateContent(ContentUtil.asContent(content), "main", ItemType.CONTENTNODE, true);
123             if (!mainCollection.hasContent(SINGLETON_NODE_NAME)) {
124                 Content singletonNode = mainCollection.createContent(SINGLETON_NODE_NAME, ItemType.CONTENTNODE);
125                 singletonNode.getMetaData().setTemplate(singletonDef.getAutoGeneratedParagraph().getName());
126 
127                 setDefaultValues(singletonNode, singletonDef.getAutoGeneratedParagraph().getDefaultValues());
128 
129                 mainCollection.save();
130             }
131         } catch (Exception e) {
132             throw new IllegalStateException("Can't create singleton paragraph", e);
133         }
134     }
135 
136     protected void setDefaultValues(Content singletonNode, Map defaultValues)
137             throws RepositoryException {
138         // set default Values
139         for (Object key : defaultValues.keySet()) {
140             String name = (String) key;
141             Object value = defaultValues.get(name);
142             singletonNode.createNodeData(name, value);
143         }
144     }
145 
146 }