View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.wcm.publication;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import javax.jcr.Node;
24  import javax.jcr.Value;
25  import javax.jcr.ValueFactory;
26  import javax.jcr.ValueFormatException;
27  
28  import org.exoplatform.portal.config.model.Application;
29  import org.exoplatform.portal.config.model.Container;
30  import org.exoplatform.portal.config.model.ModelObject;
31  import org.exoplatform.portal.config.model.Page;
32  import org.exoplatform.portal.mop.Visibility;
33  import org.exoplatform.portal.mop.navigation.NodeContext;
34  import org.exoplatform.portal.mop.user.UserNavigation;
35  import org.exoplatform.portal.mop.user.UserNode;
36  import org.exoplatform.portal.mop.user.UserNodeFilterConfig;
37  import org.exoplatform.portal.mop.user.UserPortal;
38  import org.exoplatform.portal.webui.util.Util;
39  
40  /**
41   * Created by The eXo Platform SAS
42   * Author : Hoa Pham
43   * hoa.pham@exoplatform.com
44   * Oct 2, 2008
45   */
46  public class PublicationUtil {
47  
48    /** The Constant HISTORY_SEPARATOR. */
49    public static final String HISTORY_SEPARATOR = "; ";
50  
51    /** The Constant APPLICATION_SEPARATOR. */
52    public static final String APPLICATION_SEPARATOR = "@";
53  
54    /** The Constant URI_SEPARATOR. */
55    public static final String URI_SEPARATOR = "/";
56  
57    /**
58     * Find user node by page id.
59     * @param rootNode
60     * @param pageId
61     * @return
62     * @throws Exception
63     */
64    public static List<UserNode> findUserNodeByPageId(UserNode rootNode, String pageId) throws Exception {
65      List<UserNode> allUserNodes = new ArrayList<>();
66      findUserNodeByPageId(rootNode, pageId, allUserNodes);
67      return allUserNodes;
68    }
69  
70    /**
71     * Find user node by page id.
72     * @param userNode
73     * @param pageId
74     * @param allUserNodes
75     * @throws Exception
76     */
77    public static void findUserNodeByPageId(UserNode userNode,
78                                            String pageId,
79                                            List<UserNode> allUserNodes) throws Exception {
80      Iterator<UserNode> childrenNodeIter = userNode.getChildren().iterator();
81      while (childrenNodeIter.hasNext()) {
82        UserNode node = childrenNodeIter.next();
83        if (node.getPageRef().equals(pageId)) {
84          allUserNodes.add(node);
85        } else {
86          findUserNodeByPageId(node, pageId, allUserNodes);
87        }
88      }
89    }
90  
91    /**
92     * Find app instances by name.
93     *
94     * @param page the page
95     * @param applicationName the application name
96     *
97     * @return the list of app instances
98     */
99    public static List<String> findAppInstancesByName(Page page, String applicationName) {
100     List<String> results = new ArrayList<>();
101     findAppInstancesByContainerAndName(page, applicationName, results);
102     return results;
103   }
104 
105   /**
106    * Find app instances by container and name.
107    *
108    * @param container the container
109    * @param applicationName the application name
110    * @param results the results
111    */
112   private static void findAppInstancesByContainerAndName(Container container, String applicationName, List<String> results) {
113     ArrayList<ModelObject> chidren = container.getChildren();
114     if(chidren == null) return ;
115     for(ModelObject object: chidren) {
116       if(object instanceof Application) {
117         Application<?> application = Application.class.cast(object);
118         if(application.getId().contains(applicationName)) {
119           results.add(application.getId());
120         }
121       } else if(object instanceof Container) {
122         Container child = Container.class.cast(object);
123         findAppInstancesByContainerAndName(child, applicationName, results);
124       }
125     }
126   }
127 
128   /**
129    * Removed app instances in container by names.
130    *
131    * @param container the container
132    * @param removingApplicationIds the removing application ids
133    */
134   private static void removedAppInstancesInContainerByNames(Container container,
135                                                             List<String> removingApplicationIds) {
136     ArrayList<ModelObject> childrenTmp = new ArrayList<>();
137     ArrayList<ModelObject> chidren = container.getChildren();
138     if (chidren == null)
139       return;
140     for (ModelObject object : chidren) {
141       if (object instanceof Application) {
142         Application<?> application = Application.class.cast(object);
143         if(!removingApplicationIds.contains(application.getId())) {
144           childrenTmp.add(object);
145         }
146       } else if (object instanceof Container) {
147         Container child = Container.class.cast(object);
148         removedAppInstancesInContainerByNames(child, removingApplicationIds);
149         childrenTmp.add(child);
150       }
151     }
152   }
153 
154   /**
155    * Gets the values as string.
156    *
157    * @param node the node
158    * @param propName the prop name
159    *
160    * @return the values as string
161    *
162    * @throws Exception the exception
163    */
164   public static List<String> getValuesAsString(Node node, String propName) throws Exception {
165     if(!node.hasProperty(propName)) return new ArrayList<>();
166     List<String> results = new ArrayList<>();
167     try{
168       for(Value value: node.getProperty(propName).getValues()) {
169         results.add(value.getString());
170       }
171     }catch(ValueFormatException ex){
172       results.add(node.getProperty(propName).getValue().getString());
173     }
174     return results;
175   }
176 
177   /**
178    * To values.
179    *
180    * @param factory the factory
181    * @param values the values
182    *
183    * @return the value[]
184    */
185   public static Value[] toValues(ValueFactory factory, List<String> values) {
186     List<Value> list = new ArrayList<>();
187     for(String value: values) {
188       list.add(factory.createValue(value));
189     }
190     return list.toArray(new Value[list.size()]);
191   }
192 
193   /**
194    * Removes the application from page.
195    *
196    * @param page the page
197    * @param removedApplicationIds the removed application ids
198    */
199   public static void removeApplicationFromPage(Page page, List<String> removedApplicationIds) {
200     removedAppInstancesInContainerByNames(page, removedApplicationIds);
201   }
202 
203   /**
204    * Gets the list application id by page.
205    *
206    * @param page the page
207    * @param portletName the portlet name
208    *
209    * @return the list application id by page
210    */
211   public static List<String> getListApplicationIdByPage(Page page, String portletName) {
212     return PublicationUtil.findAppInstancesByName(page, portletName);
213   }
214 
215   /**
216    * Sets the mixed navigation uri.
217    *
218    * @param portalName the portal name
219    * @param pageNodeUri the page node uri
220    *
221    * @return the string
222    */
223   public static String setMixedNavigationUri(String portalName, String pageNodeUri) {
224     return URI_SEPARATOR + portalName + URI_SEPARATOR + pageNodeUri;
225   }
226 
227   /**
228    * Parses the mixed navigation uri.
229    *
230    * @param mixedNavigationUri the mixed navigation uri
231    *
232    * @return the string[]
233    */
234   public static String[] parseMixedNavigationUri(String mixedNavigationUri) {
235     String[] mixedNavigationUris = new String[2];
236     int first = 1;
237     int second = mixedNavigationUri.indexOf(URI_SEPARATOR, first);
238     mixedNavigationUris[0] = mixedNavigationUri.substring(first, second);
239     mixedNavigationUris[1] = mixedNavigationUri.substring(second + URI_SEPARATOR.length(), mixedNavigationUri.length());
240     return mixedNavigationUris;
241   }
242 
243   /**
244    * Sets the mixed application id.
245    *
246    * @param pageId the page id
247    * @param applicationId the application id
248    *
249    * @return the string
250    */
251   public static String setMixedApplicationId(String pageId, String applicationId) {
252     return pageId + APPLICATION_SEPARATOR + applicationId;
253   }
254 
255   /**
256    * Parses the mixed application id.
257    *
258    * @param mixedApplicationId the mixed application id
259    *
260    * @return the string[]
261    */
262   public static String[] parseMixedApplicationId(String mixedApplicationId) {
263     return mixedApplicationId.split(APPLICATION_SEPARATOR);
264   }
265 
266   /**
267    * Checks if is node content published to page node.
268    *
269    * @param contentNode the content node
270    * @param navNodeURI the nav node uri
271    *
272    * @return true, if is node content published to page node
273    *
274    * @throws Exception the exception
275    */
276   public static boolean isNodeContentPublishedToPageNode(Node contentNode, String navNodeURI) throws Exception {
277 
278     UserPortal userPortal = Util.getPortalRequestContext().getUserPortalConfig().getUserPortal();
279 
280     // make filter
281     UserNodeFilterConfig.Builder filterConfigBuilder = UserNodeFilterConfig.builder();
282     filterConfigBuilder.withReadWriteCheck().withVisibility(Visibility.DISPLAYED, Visibility.TEMPORAL);
283     filterConfigBuilder.withTemporalCheck();
284     UserNodeFilterConfig filterConfig = filterConfigBuilder.build();
285 
286     // get user node
287     String nodeURI = navNodeURI.replace("/" + Util.getPortalRequestContext().getPortalOwner() + "/", "");
288     UserNavigation userNav = userPortal.getNavigation(Util.getUIPortal().getSiteKey());
289     UserNode userNode = userPortal.resolvePath(userNav, filterConfig, nodeURI);
290     
291     if (userNode == null || userNode.getPageRef() == null) return false;
292     
293     return PublicationUtil.getValuesAsString(contentNode, "publication:webPageIDs").contains(userNode.getPageRef());
294   }
295 
296   public static ArrayList<NodeContext<?>> convertAllNodeContextToList(NodeContext<?> rootNodeContext){
297     
298     if (rootNodeContext == null || rootNodeContext.getNodes() == null){
299       return null;
300     }
301 
302     ArrayList<NodeContext<?>> nodeContextList = new ArrayList<>();
303     Iterator<?> iter = rootNodeContext.getNodes().iterator();
304     while (iter.hasNext()){
305       NodeContext<?> context = (NodeContext<?>) iter.next();
306       nodeContextList.add(context);
307       nodeContextList.addAll(convertAllNodeContextToList(context));
308     }
309 
310     return nodeContextList;
311   }
312 
313   public static StringBuilder buildUserNodeURI(NodeContext<?> context) {
314     NodeContext<?> parent = (NodeContext<?>) context.getParentNode();
315      if (parent != null)
316      {
317         StringBuilder builder = buildUserNodeURI(parent);
318         if (builder.length() > 0)
319         {
320            builder.append('/');
321         }
322         return builder.append(context.getName());
323      }
324      else
325      {
326         return new StringBuilder();
327      }
328   }
329 }