1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.services.persistence;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23
24 import org.apache.jetspeed.om.profile.Entry;
25 import org.apache.jetspeed.om.profile.PSMLDocument;
26 import org.apache.jetspeed.om.profile.Portlets;
27 import org.apache.jetspeed.om.profile.Profile;
28 import org.apache.jetspeed.portal.JetspeedPortletInstance;
29 import org.apache.jetspeed.portal.Portlet;
30 import org.apache.jetspeed.portal.PortletException;
31 import org.apache.jetspeed.portal.PortletInstance;
32 import org.apache.jetspeed.services.PortletFactory;
33 import org.apache.jetspeed.services.PsmlManager;
34 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
35 import org.apache.jetspeed.services.logging.JetspeedLogger;
36 import org.apache.jetspeed.services.rundata.JetspeedRunData;
37
38
39 import org.apache.turbine.services.InitializationException;
40 import org.apache.turbine.services.TurbineBaseService;
41 import org.apache.turbine.util.RunData;
42
43 /***
44 * Implementation of the Portal Persistence Service for storing and
45 * retrieving portlet instances.
46 *
47 * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>
48 * @version $Id: JetspeedPortalPersistenceService.java,v 1.5 2004/02/23 03:33:52 jford Exp $
49 */
50 public class JetspeedPortalPersistenceService
51 extends TurbineBaseService
52 implements PortalPersistenceService
53 {
54 /***
55 * Static initialization of the logger for this class
56 */
57 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedPortalPersistenceService.class.getName());
58
59 /***
60 * Use this to verify the RunData object in use is going to be the Jetspeed
61 * RunData object. This way we know right off, whether or not things are going
62 * to work.
63 *
64 */
65 public void init(RunData data) throws InitializationException
66 {
67 if (!(data instanceof JetspeedRunData))
68 {
69 logger.error(
70 "[PortalPersistenceService] The current RunData implenetation does not implement the JetspeedRunData interface.");
71 setInit(false);
72 throw new InitializationException("The current RunData implenetation does not implement the JetspeedRunData interface.");
73 }
74
75 super.init(data);
76 }
77
78 /***
79 * @see PortalPersistenceService#getInstance(Portlet, Profile)
80 */
81 public PortletInstance getInstance(Portlet portlet, Profile profile)
82 {
83 return new JetspeedPortletInstance(portlet, profile);
84 }
85
86 /***
87 * @see PortalPersistenceService#getInstance(Portlet, RunData)
88 */
89 public PortletInstance getInstance(Portlet portlet, RunData data)
90 {
91 String attrKey = "portlet_instance:"+portlet.getID();
92
93
94
95 PortletInstance instance = (PortletInstance) data.getRequest().getAttribute(attrKey);
96 if(instance != null)
97 {
98 return instance;
99 }
100 else
101 {
102 instance= new JetspeedPortletInstance(portlet, data);
103 data.getRequest().setAttribute(attrKey, instance);
104 return instance;
105 }
106
107 }
108
109 /***
110 * @see PortalPersistenceService#store(PortletInstance)
111 */
112 public void store(PortletInstance pPortlet) throws PortalPersistenceException
113 {
114 Profile profile = pPortlet.getProfile();
115 profile.setDocument(pPortlet.getDocument());
116
117 if(!PsmlManager.store(profile))
118 {
119 throw new PortalPersistenceException("Unable to write portlet information.");
120 }
121 }
122
123 /***
124 * @see PortalPersistenceService#getInstances(Portlet, Profile)
125 */
126 public List getInstances( Profile profile) throws PortletException
127 {
128 PSMLDocument doc = profile.getDocument();
129 Portlets portlets = doc.getPortlets();
130 ArrayList pList = new ArrayList();
131
132 buildAllEntries(portlets, pList, profile);
133
134 return pList;
135 }
136
137 /***
138 * @see PortalPersistenceService#getInstances(Portlet, RunData)
139 */
140 public List getInstances( RunData data) throws PortletException
141 {
142 JetspeedRunData jData = (JetspeedRunData) data;
143 return getInstances(jData.getProfile());
144 }
145
146 protected void buildAllEntries(Portlets portlets, ArrayList entries, Profile profile) throws PortletException
147 {
148
149 Iterator eItr = portlets.getEntriesIterator();
150 while(eItr.hasNext())
151 {
152 Object obj = eItr.next();
153 Entry entry = (Entry)obj;
154 Portlet portlet = PortletFactory.getPortlet(entry);
155 entries.add(new JetspeedPortletInstance(portlet, profile));
156 }
157
158
159 if(portlets.getPortletsCount() > 0)
160 {
161 Iterator pItr = portlets.getPortletsIterator();
162 while(pItr.hasNext())
163 {
164 Portlets childPortlets = (Portlets)pItr.next();
165 buildAllEntries(childPortlets, entries, profile);
166 }
167
168 return;
169 }
170 else
171
172 {
173 return;
174 }
175
176
177 }
178
179 }