View Javadoc

1   package de.iqser.portlets;
2   
3   import java.io.FileInputStream;
4   import java.io.FileNotFoundException;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.util.Enumeration;
8   import java.util.Properties;
9   
10  import javax.portlet.ActionRequest;
11  import javax.portlet.ActionResponse;
12  import javax.portlet.GenericPortlet;
13  import javax.portlet.PortletException;
14  import javax.portlet.PortletPreferences;
15  import javax.portlet.PortletRequest;
16  import javax.portlet.PortletRequestDispatcher;
17  import javax.portlet.RenderRequest;
18  import javax.portlet.RenderResponse;
19  
20  public class IqserBasePortlet extends GenericPortlet {
21  
22  	public final static String PARAM_ACTION= "PARAM_ACTION";
23  	// portlet actions
24  	public final static String ACTION_FILTER= "ACTION_FILTER";
25  	public final static String ACTION_INSERT_PREF= "ACTION_INSERT_PREF";
26  	public final static String ACTION_PREVIEW= "ACTION_PREVIEW";
27  	public final static String ACTION_SEARCH= "ACTION_SEARCH";
28  	public final static String ACTION_SHOW_BREADCRUMB= "ACTION_SHOW_BREADCRUMB";
29  	public final static String ACTION_SHOW_RELATED= "ACTION_SHOW_RELATED";
30  	public final static String ACTION_UPDATE_PREFS= "ACTION_UPDATE_PREFS";
31  	
32  	// parameters
33  	public final static String PARAM_CONTENT_TYPE= "PARAM_CONTENT_TYPE";
34  	public final static String PARAM_CONTENT_PROVIDER= "PARAM_CONTENT_PROVIDER";
35  	public final static String PARAM_ID= "PARAM_ID";
36  	public final static String PARAM_TITLE= "PARAM_TITLE";
37  	public final static String PARAM_SEARCH_INPUT= "PARAM_SEARCH_INPUT";
38  	public final static String PARAM_TYPES= "PARAM_TYPES";
39  	public final static String PARAM_SELECTED_TYPES= "PARAM_SELECTED_TYPES";
40  	public final static String PARAM_ERROR_MESSAGE= "ERROR_MESSAGE";
41  	public final static String PARAM_RELATED_LIST= "PARAM_RELATED_LIST";
42  	
43  	public final static String PUBLIC_RENDER_PARAM_SEARCH_INPUT= "searchInput";
44  	public final static String PUBLIC_RENDER_PARAM_CONTENT_ID= "contentId";
45  	public final static String PUBLIC_RENDER_PARAM_CONTENT_TYPE= "contentType";
46  	public final static String PUBLIC_RENDER_PARAM_CONTENT_PROVIDER= "contentProvider";
47  	
48  	// common portlet preferences
49  	public final static String PREF_IQSER_WEBSERVICE= "iQser Webservice";
50  	
51  	// session parameters
52  	public final static String DEIQSER_CACHED_DATA= "DEIQSER_CACHED_DATA";
53  	public final static String DEIQSER_RELATED_LIST= "DEIQSER_RELATED_LIST";
54  	public final static String DEIQSER_SEARCH_FIELD= "DEIQSER_SEARCH_FIELD";
55  	public final static String DEIQSER_SELECTED_TYPES= "DEIQSER_SELECTED_TYPES";
56  	public final static String DEIQSER_TREE_POSITION= "DEIQSER_TREE_POSITION";
57  	
58  	public final static String HIDE_SYSTEM_PREFERENCES= "HIDE_SYSTEM_PREFERENCES";
59  	
60  	protected static Properties props = null;
61  
62  	private static final String _APPID = "IQSER";
63  	
64  	// Portlet init-Params
65  	protected String viewJSP;
66  	protected String helpJSP;
67  	protected String editJSP;
68  
69  	// out of config.properties
70  	protected boolean isDebug = false;
71  
72  	// Help articleId
73  	protected String _helpArticleId = null;
74  
75  	private String hideSystemProperties = "true";     
76  
77  	public void doEdit(RenderRequest req, RenderResponse res)
78  			throws IOException, PortletException {
79  
80  		if (hideSystemProperties != null) {
81  			req.setAttribute(HIDE_SYSTEM_PREFERENCES, hideSystemProperties);
82  		}
83  
84  		PortletPreferences prefs = req.getPreferences();
85  		req.setAttribute("PortletPrefs", prefs);
86  
87  		include(editJSP, req, res);
88  
89  	}
90  
91  	// a simple process action for preference handling
92  	// if more is needed, the child portlet must override this method
93  	// If the preference handling mechanism is needed, the overriding method can
94  	// call
95  	// the helper methods processActionUpdatePrefs and processActionInsertPref
96  	public void processAction(ActionRequest actionRequest,
97  			ActionResponse actionResponse) throws PortletException, IOException {
98  
99  		String action = actionRequest.getParameter(PARAM_ACTION);
100 
101 		if (action != null && action.equals(ACTION_UPDATE_PREFS)) {
102 			processActionUpdatePrefs(actionRequest);
103 			return;
104 		} else if (action != null && action.equals(ACTION_INSERT_PREF)) {
105 			processActionInsertPref(actionRequest, actionResponse);
106 			return;
107 		}
108 
109 	}
110 
111 	protected void processActionUpdatePrefs(ActionRequest actionRequest)
112 			throws PortletException, IOException {
113 		// first update all entries
114 		PortletPreferences prefs = actionRequest.getPreferences();
115 		Enumeration<String> enu1 = actionRequest.getParameterNames();
116 		while (enu1.hasMoreElements()) {
117 			String key = (String) enu1.nextElement();
118 			if (key.startsWith("###")) {
119 				String value = (String) actionRequest.getParameter(key);
120 				key = key.substring(3); // remove ###
121 				// System.out.println(key + " = " + value);
122 				prefs.setValue(key, value);
123 			}
124 		}
125 		Enumeration<String> enu2 = actionRequest.getParameterNames();
126 		while (enu2.hasMoreElements()) {
127 			// delete marked entries
128 			String key = (String) enu2.nextElement();
129 			if (key.startsWith("#-#")) {
130 				// a parameter which has to be deleted
131 				String keyToDelete = key.substring(3); // remove #-#
132 				// System.out.println("Delete :" + keyToDelete);
133 				prefs.reset(keyToDelete);
134 			}
135 		}
136 		prefs.store();
137 	}
138 
139 	protected void processActionInsertPref(ActionRequest actionRequest,
140 			ActionResponse actionResponse) throws PortletException, IOException {
141 
142 		String key = actionRequest.getParameter("insertPrefKey");
143 		String value = actionRequest.getParameter("insertPrefValue");
144 		// System.out.println("Insert :" + key + " = " + value);
145 		if (key != null && key.length() > 0) {
146 			// check if the special key TMSP was used
147 			if (key.equals("TMSP")) {
148 				// this is not a real preference, it is used to switch on or off
149 				// the display of system preferences
150 				if (value.equalsIgnoreCase("ON")) {
151 					hideSystemProperties = "false";
152 				} else {
153 					hideSystemProperties = "true";
154 				}
155 				return;
156 			}
157 			PortletPreferences prefs = actionRequest.getPreferences();
158 			prefs.setValue(key, value);
159 			prefs.store();
160 		}
161 	}
162 
163 	protected void include(String path, RenderRequest req, RenderResponse res)
164 			throws IOException, PortletException {
165 		System.out.println("JSP-Path:" + path);
166 		PortletRequestDispatcher prd = getPortletContext()
167 				.getRequestDispatcher(path);
168 
169 		if (prd == null) {
170 			System.out.println( path + " is not a valid include");
171 		} else {
172 			prd.include(req, res);
173 		}
174 	}
175 
176 
177 	protected Properties getProperties() {
178 		String workingDirectory = getWorkingDirectory();
179 		String fullFileName = workingDirectory + "/config/config.properties";
180 		InputStream is = null;
181 		try {
182 			is = new FileInputStream(fullFileName);
183 		} catch (FileNotFoundException ex) {
184 			System.out.println("Exception while loading properties from :"
185 					+ fullFileName);
186 		}
187 		try {
188 			if (props == null) {
189 				props = new Properties();
190 			}
191 			props.load(is);
192 			is.close();
193 		} catch (IOException ex) {
194 			System.out.println("Exception while loading properties from :"
195 					+ fullFileName);
196 		}
197 		return props;
198 	}
199 
200 	private String getWorkingDirectory() {
201 		String workingDirectory = System.getProperty("working.directory") + "/" + _APPID;
202 		// String workingDirectory= System.getProperty(_WORKING_DIRECTORY);
203 		return workingDirectory;
204 	}
205 
206 	protected String getProperty(PortletRequest request, String key) {
207 		getProperties();
208 		String value = props.getProperty(key);
209 		if (value == null) {
210 			System.out.println("Property " + key
211 					+ " was not found in the property file");
212 			return "";
213 		} else {
214 			return value;
215 		}
216 	}
217 
218 	protected void printRequestData(PortletRequest request) {
219  		System.out.println("\n************************");
220  		System.out.println("Parameters");
221  		Enumeration<String> enu= request.getParameterNames();
222  		while( enu.hasMoreElements() ) {
223  			String key= (String) enu.nextElement();
224  			Object value= request.getParameter(key);
225  			System.out.println(key + " = " + value);
226  		}
227  		
228  		System.out.println("Attributes");
229  		enu= request.getAttributeNames();
230  		while( enu.hasMoreElements() ) {
231  			String key= (String) enu.nextElement();
232  			Object value= request.getAttribute(key);
233  			System.out.println(key + " = " + value);
234  		}
235  		System.out.println("************************\n\n");
236  	}
237 }