View Javadoc

1   /**
2    * This file Copyright (c) 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.googlesitemap.model;
35  
36  import info.magnolia.cms.core.MgnlNodeType;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.jcr.predicate.AbstractPredicate;
39  import info.magnolia.jcr.util.MetaDataUtil;
40  import info.magnolia.jcr.util.NodeUtil;
41  import info.magnolia.jcr.util.SessionUtil;
42  import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration;
43  import info.magnolia.module.googlesitemap.bean.SiteMapEntry;
44  import info.magnolia.module.googlesitemap.service.SiteMapService;
45  import info.magnolia.rendering.model.RenderingModel;
46  import info.magnolia.rendering.model.RenderingModelImpl;
47  import info.magnolia.rendering.template.RenderableDefinition;
48  
49  import java.util.HashSet;
50  import java.util.Iterator;
51  import java.util.Set;
52  
53  import javax.inject.Inject;
54  import javax.jcr.Node;
55  import javax.jcr.NodeIterator;
56  import javax.jcr.Property;
57  import javax.jcr.PropertyIterator;
58  import javax.jcr.RepositoryException;
59  
60  import org.apache.commons.lang.StringUtils;
61  import org.apache.jackrabbit.commons.predicate.Predicate;
62  import org.slf4j.Logger;
63  import org.slf4j.LoggerFactory;
64  
65  
66  /**
67   * Main Model class of the siteMap Templates.
68   *
69   * @version $Id$
70   * @param <RD>.
71   */
72  public class SiteMapModel <RD extends RenderableDefinition> extends RenderingModelImpl<RD> {
73  
74      // Global and static variable section.
75      private static final Logger log = LoggerFactory.getLogger(SiteMapModel.class);
76  
77      // Used to filter the current node and just get child nodes starting with SITE_DIALOG_CONFIGURATION_NAME
78      private static Predicate All_Site_Nodes = new AbstractPredicate<Node>() {
79          @Override
80          public boolean evaluateTyped(Node node) {
81              try {
82                  return node.getName().startsWith(GoogleSiteMapConfiguration.SITE_DIALOG_CONFIGURATION_NAME);
83              } catch (RepositoryException e) {
84                  return false;
85              }
86          }
87      };
88  
89      //Injected service.
90      protected SiteMapService siteMapService;
91  
92  
93      /**
94       * Default constructor used for injection.
95       */
96      @Inject
97      public SiteMapModel(Node content, RD definition, RenderingModel< ? > parent, SiteMapService siteMapService) {
98          super(content, definition, parent);
99          this.siteMapService = siteMapService;
100     }
101 
102 
103     @Override
104     public String execute() {
105 
106         // For the siteMap display, set the content type to text/xml.
107         if(this.definition.getParameters()!=null && this.definition.getParameters().containsKey("xmlDisplay")) {
108             MgnlContext.getWebContext().getResponse().setContentType("text/xml");
109         }
110 
111         return null;
112     }
113 
114 
115     /**
116      * Get Site and virtualUri informations if they are defined as components.
117      * Notice that by using a HashSet, a single object will be keeped in case of equality of the location.
118      */
119     public Iterator<SiteMapEntry> getSiteMapBeans() {
120         Set<SiteMapEntry> res = new HashSet<SiteMapEntry>();
121         try {
122             // Return if no content set.
123             if(!content.hasNode("content")) {
124                 return res.iterator();
125             }
126 
127             //Display Site informations part... if defined
128             Node node = null;
129             Iterator<Node> iterator = NodeUtil.collectAllChildren(content,All_Site_Nodes).iterator();
130             while (iterator.hasNext()) {
131                 //Get node
132                 node = iterator.next();
133                 PropertyIterator properties = node.getProperties();
134 
135                 while(properties.hasNext()) {
136                     Property property = properties.nextProperty();
137                     if(!property.getName().startsWith(MgnlNodeType.JCR_PREFIX)) {
138                         Node root = SessionUtil.getNode(this.content.getSession().getWorkspace().getName(), property.getString());
139                         //Call service and  Remove Duplicate
140                         res.addAll(siteMapService.getSiteMapBeanForSite(root, false));
141                     }
142                 }
143             }
144 
145 
146             //Display VirtualUriInformation informations part... if defined
147             NodeIterator nodeIterator = content.getNode("content").getNodes();
148             while (nodeIterator.hasNext()) {
149                 node = nodeIterator.nextNode();
150                 String templateName = MetaDataUtil.getTemplate(node);
151                 if(StringUtils.isNotEmpty(templateName) && templateName.endsWith(GoogleSiteMapConfiguration.VIRTUAL_URI_TEMPLATE_NAME)) {
152                     // Call service
153                     res.addAll(siteMapService.getSiteMapBeanForVirtualUri(false));
154                     break;
155                 }
156             }
157 
158 
159         } catch (RepositoryException re) {
160             log.error("Could not initialize SiteMapBeans", re);
161         }
162         return res.iterator();
163     }
164 
165 
166 
167 }