KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > content > ContentUtil


1 /**
2  * Copyright 2001-2004 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.portlets.content;
6
7 import java.util.Locale JavaDoc;
8
9 import javax.faces.context.ExternalContext;
10 import javax.faces.context.FacesContext;
11 import javax.jcr.Credentials;
12 import javax.jcr.Node;
13 import javax.jcr.PathNotFoundException;
14 import javax.jcr.Property;
15 import javax.jcr.Repository;
16 import javax.jcr.RepositoryException;
17 import javax.jcr.SimpleCredentials;
18 import javax.jcr.StringValue;
19 import javax.jcr.Ticket;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.exoplatform.container.PortalContainer;
23 import org.exoplatform.container.SessionContainer;
24 import org.exoplatform.portal.session.PortalResources;
25 import org.exoplatform.portlets.content.display.component.model.ContentConfig;
26 import org.exoplatform.services.jcr.RepositoryService;
27 import org.exoplatform.services.organization.OrganizationService;
28 import org.exoplatform.services.organization.User;
29
30 /**
31  * @author Ove Ranheim (oranheim@yahoo.no)
32  * @since Jul 5, 2004 1:35:46 PM
33  */

34 public class ContentUtil {
35
36   public static final String JavaDoc EDIT_STATE = "ACTION_EDIT";
37
38   public static final String JavaDoc MODIFY_STATE = "ACTION_MODIFY";
39
40   private static final String JavaDoc JCR_ROOT_PATH = "/cms/home/";
41
42   private static final String JavaDoc WORSPACE = "ws";
43
44   private static final String JavaDoc DEFAULT_LOCALE = "en";
45
46   private static final String JavaDoc ROOT_FILE_NAME = "home";
47
48   public static Ticket getCurrentTicket(String JavaDoc userName) {
49     try {
50       PortalContainer manager = PortalContainer.getInstance();
51       RepositoryService repositoryService = (RepositoryService) manager
52           .getComponentInstanceOfType(RepositoryService.class);
53       Repository repository = repositoryService.getRepository();
54       return repository.login(null, WORSPACE);
55     } catch (RepositoryException e) {
56       throw new RuntimeException JavaDoc(e);
57     } catch (Exception JavaDoc e) {
58       throw new RuntimeException JavaDoc(e);
59     }
60   }
61
62   public static String JavaDoc resolveContent(ContentConfig config,
63       org.exoplatform.services.portal.model.Node currentNode) {
64     try {
65       String JavaDoc uri = config.getUri();
66       String JavaDoc pageRef = currentNode.getPageReference("text/xhtml").getPageReference();
67       String JavaDoc[] splitedPageRef = StringUtils.split(pageRef, ":");
68       String JavaDoc owner = splitedPageRef[0];
69       if (uri == null || "".equals(uri)) {
70         //use the currentNodePath to lookup the content
71
uri = splitedPageRef[1];
72       }
73       Node node = lookupNode(getCurrentTicket(getUserName()), owner, uri, true);
74       Property prop = node.getNode("jcr:content").getProperty("exo:content");
75       if (prop != null)
76         return prop.getString();
77     } catch (PathNotFoundException e) {
78       return null;
79     } catch (RepositoryException e) {
80       //log_.error(e.getMessage());
81
throw new RuntimeException JavaDoc(e);
82     }
83     return null;
84   }
85
86   private static Node lookupNode(Ticket ticket, String JavaDoc owner, String JavaDoc uri, boolean useDefault)
87       throws PathNotFoundException, RepositoryException {
88     String JavaDoc[] splittedName = StringUtils.split(uri, "/");
89     String JavaDoc realNameFile = null;
90     if (splittedName.length == 0) {
91       realNameFile = ROOT_FILE_NAME;
92     } else {
93       realNameFile = splittedName[splittedName.length - 1];
94     }
95     uri = JCR_ROOT_PATH + owner + uri;
96     Node node = ticket.getNodeByAbsPath(uri);
97     try {
98       node = node.getNode(realNameFile + "_" + getLocale().getLanguage()
99           + ".html");
100     } catch (PathNotFoundException e) {
101       if(useDefault)
102         node = node.getNode(realNameFile + "_" + DEFAULT_LOCALE + ".html");
103       else
104         throw e;
105     }
106     return node;
107   }
108
109   public static Node createNode(Ticket ticket, String JavaDoc uri)
110     throws RepositoryException {
111     Node node = getHomeNode(ticket);
112     return createNodeFromNode(node, uri);
113   }
114   
115   public static Node createContentNode(Ticket ticket, String JavaDoc uri)
116       throws RepositoryException {
117     Node node = getHomeNode(ticket);
118     node = createNodeFromNode(node, uri);
119     String JavaDoc[] splittedName = StringUtils.split(uri, "/");
120     node = node.addNode(splittedName[splittedName.length - 1] + "_"
121         + getLocale().getLanguage() + ".html", "nt:file");
122     return node;
123   }
124   
125   private static Node createNodeFromNode(Node node, String JavaDoc uri) throws RepositoryException{
126     String JavaDoc[] splittedName = StringUtils.split(uri, "/");
127     for (int i = 0; i < splittedName.length; i++) {
128       try {
129         node = node.getNode(splittedName[i]);
130       } catch (PathNotFoundException exc) {
131         node = node.addNode(splittedName[i], "nt:folder");
132       }
133     }
134     return node;
135     
136   }
137
138   private static Node getHomeNode(Ticket ticket) throws PathNotFoundException,
139       RepositoryException {
140     String JavaDoc homeDir = JCR_ROOT_PATH + getPortalOwner();
141     Node node = null;
142     try {
143       node = ticket.getNodeByAbsPath(homeDir);
144     } catch(PathNotFoundException ex) {
145       node = createNodeFromNode(ticket.getRootNode(), homeDir);
146       ticket.save();
147     }
148     return node;
149   }
150
151   private static Locale JavaDoc getLocale() {
152     PortalResources appres =
153       (PortalResources)SessionContainer.getComponent(PortalResources.class);
154     return appres.getLocale();
155   }
156
157   private static String JavaDoc getUserName() {
158     ExternalContext econtext = FacesContext.getCurrentInstance()
159         .getExternalContext();
160     return econtext.getRemoteUser();
161   }
162
163   private static String JavaDoc getPortalOwner() {
164     return SessionContainer.getInstance().getOwner() ;
165   }
166
167   public static void storeContent(String JavaDoc uri, String JavaDoc content)
168       throws RepositoryException {
169     String JavaDoc[] splittedName = StringUtils.split(uri, "/");
170     ExternalContext econtext = FacesContext.getCurrentInstance()
171         .getExternalContext();
172     String JavaDoc username = econtext.getRemoteUser();
173     Ticket ticket = getCurrentTicket(username);
174     Node node = null;
175     try {
176       node = lookupNode(ticket, getPortalOwner(), uri, false);
177     } catch (PathNotFoundException e) {
178       node = createContentNode(ticket, uri);
179     }
180     node = node.getNode("jcr:content");
181     node.setProperty("exo:content", new StringValue(content));
182     getHomeNode(ticket).getNode(splittedName[0]).save(false);
183   }
184 }
Popular Tags