View Javadoc

1   /*
2    * Copyright 2003,2004,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.pluto.portlet.admin.services;
17  
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.portlet.ActionRequest;
28  import javax.portlet.PortletSession;
29  import javax.portlet.RenderRequest;
30  
31  import org.apache.pluto.portalImpl.om.entity.impl.PortletApplicationEntityImpl;
32  import org.apache.pluto.portalImpl.om.entity.impl.PortletApplicationEntityListImpl;
33  import org.apache.pluto.portlet.admin.BaseAdminObject;
34  import org.apache.pluto.portlet.admin.PlutoAdminConstants;
35  import org.apache.pluto.portlet.admin.PlutoAdminException;
36  import org.apache.pluto.portlet.admin.PlutoAdminLogger;
37  import org.apache.pluto.portlet.admin.util.PortletApplicationEntityImplComparator;
38  
39  /***
40   * Service concerned with portletentityregistry.xml persistence
41   *
42   * @author Craig Doremus
43   * @see org.apache.pluto.portlet.admin.controller.PortletEntityRegistryPortlet
44   *
45   */
46  public class PortletRegistryService extends BaseAdminObject {
47  
48  	private static final String CLASS_NAME = "PortletRegistryService";
49  
50  
51  	/***
52  	 * Default constructor
53  	 */
54  	public PortletRegistryService() {
55  		super(CLASS_NAME);
56  	}
57  
58  	public List getPageRegistryData(String prPath) {
59  		return null;
60  	}
61  
62  	/*
63  	public void savePortlet(String perPath, ApplicationTypeImpl app){
64  		final String METHOD_NAME = "savePortlet(perPath,app)";
65  		logMethodStart(METHOD_NAME);
66  		logParam(METHOD_NAME, "perPath", perPath);
67  		logParam(METHOD_NAME, "app", app);
68  		PortletEntityRegistryXao xao = new PortletEntityRegistryXao();
69  		xao.saveApplications(perPath,app);
70  		logMethodEnd(METHOD_NAME);
71  	}
72  		*/
73  
74  	public List getPortletEntityRegistry(String perPath) {
75  		final String METHOD_NAME = "getPortletEntityRegistry(perPath)";
76  		logMethodStart(METHOD_NAME);
77  		logParam(METHOD_NAME, "perPath", perPath);
78  		List alist = null;
79  		/*
80  		PortletEntityRegistryXao xao = new PortletEntityRegistryXao();
81  		alist = xao.getApplications(perPath);
82  		Iterator iter = alist.iterator();
83  		while (iter.hasNext()) {
84  			ApplicationTypeImpl app = (ApplicationTypeImpl) iter.next();
85  			logVar(METHOD_NAME, "appId", app.getId());
86  		}
87  		logMethodEnd(METHOD_NAME, alist);
88  		*/
89  		return alist;
90  	}
91  
92  	/*
93  	public ApplicationTypeImpl getPortletEntityRegistryApp(String perPath, String appId) {
94  		final String METHOD_NAME = "getPortletEntityRegistryApp(perPath, appId)";
95  		logMethodStart(METHOD_NAME);
96  		logParam(METHOD_NAME, "perPath", perPath);
97  		logParam(METHOD_NAME, "appId", appId);
98  		ApplicationTypeImpl app = null;
99  		PortletEntityRegistryXao xao = new PortletEntityRegistryXao();
100 		app = xao.getApplication(perPath, appId);
101 		if (app != null) {
102 			logDebug(METHOD_NAME, "looked up appId=" + app.getId());
103 		}
104 		logMethodEnd(METHOD_NAME, app);
105 		return app;
106 	}
107 		*/
108 
109 		public List getPortletPreferences(String perPath, String appId, String portletId ) {
110 			final String METHOD_NAME = "getPortletPreferences(perPath,appId,portletId)";
111 			logMethodStart(METHOD_NAME);
112 			List list = null;
113 			/*
114 			PortletEntityRegistryXao xao = new PortletEntityRegistryXao();
115 			list = xao.getPortletPreferencesList(perPath, appId, portletId);
116 			logMethodEnd(METHOD_NAME, list);
117 */
118 			return list;
119 		}
120 
121 		public List addPortletPreference(String perPath, String appId, String portletId ) {
122 			final String METHOD_NAME = "addPortletPreferences(perPath,appId,portletId)";
123 			logMethodStart(METHOD_NAME);
124 			List list = null;
125 			/*
126 			list = getPortletPreferences(perPath, appId, portletId);
127 			//create a new empty preference and add it
128 			PreferencesTypeImpl pref = new PreferencesTypeImpl();
129 			pref.setPrefName("");
130 			pref.setPrefValue("");
131 			pref.setReadOnly(false);
132 			list.add(pref);
133 			logMethodEnd(METHOD_NAME, list);
134 			*/
135 			return list;
136 		}
137 
138 /*
139 		public PortletTypeImpl getNewPortlet(String perPath, String appId) {
140 			final String METHOD_NAME = "getNewPortlet(perPath,appId,portletId)";
141 			logMethodStart(METHOD_NAME);
142 			PortletTypeImpl portlet = null;
143 			PortletEntityRegistryXao xao = new PortletEntityRegistryXao();
144 			ApplicationTypeImpl app = xao.getApplication(perPath, appId);
145 			portlet.setDefinitionId("");
146 			portlet.setId("");
147 			logMethodEnd(METHOD_NAME, portlet);
148 			return portlet;
149 		}
150 */
151 
152 		public static String getNextAppId() {
153 			final String METHOD_NAME = "getNextAppId()";
154 			PlutoAdminLogger.logMethodStart(CLASS_NAME, METHOD_NAME);
155 			String appId = null;
156 			Collection apps;
157 			try {
158 				org.apache.pluto.portlet.admin.model.PortletEntityRegistryXao xao =
159 					new org.apache.pluto.portlet.admin.model.PortletEntityRegistryXao();
160 				apps = xao.getApplications();
161 			} catch (Exception e) {
162 				PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
163 				throw new PlutoAdminException(e);
164 			}
165 			ArrayList list = new ArrayList(apps);
166 			Iterator iter = list.iterator();
167 			int nNewId = 0;
168 			while (iter.hasNext()) {
169 				PortletApplicationEntityImpl app = (PortletApplicationEntityImpl) iter.next();
170 				String currAppId = app.getCastorId();
171 				int nCurrAppId = Integer.parseInt(currAppId);
172 				if (nNewId <= nCurrAppId) {
173 					nNewId = nCurrAppId;
174 				}
175 			}
176 			nNewId++;
177 			appId = Integer.toString(nNewId);
178 			PlutoAdminLogger.logMethodEnd(CLASS_NAME, METHOD_NAME, appId);
179 			return appId;
180 
181 		}
182 
183 		public void getPortletEntityRegistry(RenderRequest request) {
184 			final String METHOD_NAME = "getPortletEntityRegistry(request)";
185 			logMethodStart(METHOD_NAME);
186 			Collection alist = null;
187 			try {
188 				org.apache.pluto.portlet.admin.model.PortletEntityRegistryXao xao =
189 					new org.apache.pluto.portlet.admin.model.PortletEntityRegistryXao();
190 				PortletApplicationEntityListImpl registry = xao.load();
191 				alist = registry.getCastorApplications();
192 				logDebug(METHOD_NAME, "App list: " + alist);
193 			} catch (Exception e) {
194 				logError(METHOD_NAME, e);
195 				throw new PlutoAdminException(e);
196 			}
197 			//sort the collection
198 			ArrayList slist = new ArrayList(alist);
199 			Collections.sort(slist, new PortletApplicationEntityImplComparator());
200 			Iterator iter = slist.iterator();
201 			request.setAttribute(PlutoAdminConstants.PER_LIST_ATTR, iter);
202 			logMethodEnd(METHOD_NAME, alist);
203 		}
204 
205 		public void getPortletEntityRegistryApp(ActionRequest request) {
206 			final String METHOD_NAME = "getPortletEntityRegistryApp(request)";
207 			logMethodStart(METHOD_NAME);
208 			String appId = request.getParameter("appid");
209 			logDebug(METHOD_NAME, "AppId selected: " + appId);
210 			PortletSession session = request.getPortletSession();
211 			PortletApplicationEntityImpl app;
212 			try {
213 				org.apache.pluto.portlet.admin.model.PortletEntityRegistryXao xao =
214 					new org.apache.pluto.portlet.admin.model.PortletEntityRegistryXao();
215 				app = xao.getApplication(appId);
216 			} catch (Exception e) {
217 				logError(METHOD_NAME, e);
218 				throw new PlutoAdminException(e);
219 			}
220 			session.setAttribute(PlutoAdminConstants.APP_ATTR, app, PortletSession.APPLICATION_SCOPE);
221 			logMethodEnd(METHOD_NAME);
222 		}
223 
224 		public void getPortletPreferences(ActionRequest request) {
225 			String METHOD_NAME = "getPortletPreferences(request)";
226 			logMethodStart(METHOD_NAME);
227 			String appId = request.getParameter("appId");
228 			logDebug(METHOD_NAME, "AppId selected: " + appId);
229 			String portletId = request.getParameter("portletId");
230 			logDebug(METHOD_NAME, "PortletId selected: " + portletId);
231 			List prefs = null;
232 			try {
233 				org.apache.pluto.portlet.admin.model.PortletEntityRegistryXao xao =
234 					new org.apache.pluto.portlet.admin.model.PortletEntityRegistryXao();
235 				Collection coll = xao.getPortletPreferences(appId, portletId);
236 				prefs = new ArrayList(coll);
237 			} catch (Exception e) {
238 				logError(METHOD_NAME, e);
239 				throw new PlutoAdminException(e);
240 			}
241 			PortletSession session = request.getPortletSession();
242 			Map map = (Map)session.getAttribute(PlutoAdminConstants.PREF_LIST_ATTR);
243 			if (map == null) {
244 				map = new HashMap();
245 			}
246 			map.put(portletId, prefs);
247 			session.setAttribute(PlutoAdminConstants.PREF_LIST_ATTR, map, PortletSession.APPLICATION_SCOPE);
248 			logMethodEnd(METHOD_NAME);
249 		}
250 }