001 /*----------------------------------------------------------------------------
002 This file is part of deegree, http://deegree.org/
003 Copyright (C) 2001-2009 by:
004 Department of Geography, University of Bonn
005 and
006 lat/lon GmbH
007
008 This library is free software; you can redistribute it and/or modify it under
009 the terms of the GNU Lesser General Public License as published by the Free
010 Software Foundation; either version 2.1 of the License, or (at your option)
011 any later version.
012 This library is distributed in the hope that it will be useful, but WITHOUT
013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
014 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
015 details.
016 You should have received a copy of the GNU Lesser General Public License
017 along with this library; if not, write to the Free Software Foundation, Inc.,
018 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019
020 Contact information:
021
022 lat/lon GmbH
023 Aennchenstr. 19, 53177 Bonn
024 Germany
025 http://lat-lon.de/
026
027 Department of Geography, University of Bonn
028 Prof. Dr. Klaus Greve
029 Postfach 1147, 53001 Bonn
030 Germany
031 http://www.geographie.uni-bonn.de/deegree/
032
033 e-mail: info@deegree.org
034 ----------------------------------------------------------------------------*/
035
036 package org.deegree.portal.portlet.modules.map.actions.portlets;
037
038 import java.io.File;
039 import java.io.FileOutputStream;
040 import java.io.IOException;
041 import java.io.OutputStreamWriter;
042 import java.io.UnsupportedEncodingException;
043 import java.net.MalformedURLException;
044 import java.net.URLDecoder;
045 import java.util.ArrayList;
046 import java.util.List;
047
048 import javax.servlet.ServletContext;
049 import javax.servlet.http.HttpServletRequest;
050 import javax.servlet.http.HttpSession;
051 import javax.xml.parsers.ParserConfigurationException;
052
053 import org.apache.jetspeed.portal.Portlet;
054 import org.deegree.enterprise.WebUtils;
055 import org.deegree.framework.log.ILogger;
056 import org.deegree.framework.log.LoggerFactory;
057 import org.deegree.framework.util.ConvenienceFileFilter;
058 import org.deegree.framework.util.StringTools;
059 import org.deegree.framework.xml.NamespaceContext;
060 import org.deegree.framework.xml.XMLFragment;
061 import org.deegree.framework.xml.XMLParsingException;
062 import org.deegree.framework.xml.XMLTools;
063 import org.deegree.i18n.Messages;
064 import org.deegree.model.crs.UnknownCRSException;
065 import org.deegree.ogcbase.CommonNamespaces;
066 import org.deegree.portal.PortalException;
067 import org.deegree.portal.context.ContextException;
068 import org.deegree.portal.context.General;
069 import org.deegree.portal.context.ViewContext;
070 import org.deegree.portal.context.WMCMetadataSurrogate;
071 import org.deegree.portal.context.WebMapContextFactory;
072 import org.deegree.portal.context.XMLFactory;
073 import org.deegree.portal.portlet.modules.actions.AbstractPortletPerform;
074 import org.deegree.portal.portlet.modules.actions.IGeoPortalPortletPerform;
075 import org.deegree.security.drm.model.User;
076 import org.w3c.dom.Element;
077 import org.xml.sax.SAXException;
078
079 /**
080 * Perform class for saving WMCs
081 *
082 * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
083 * @author last edited by: $Author: taddei $
084 *
085 * @version $Revision: 1.9 $, $Date: 2007/01/04 11:18:33 $
086 */
087
088 public class WMCManagementPortletPerfom extends IGeoPortalPortletPerform {
089
090 private static final ILogger LOG = LoggerFactory.getLogger( WMCManagementPortletPerfom.class );
091
092 public static final String TITLE = "TITLE";
093
094 public static final String DESCRIPTION = "DESCRIPTION";
095
096 public static final String KEYWORDS = "KEYWORDS";
097
098 public static final String FILENAME = "FILENAME";
099
100 private static NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
101
102 private static final List<WMCMetadataSurrogate> sharedContextList = new ArrayList<WMCMetadataSurrogate>( 50 );
103
104 /**
105 * constructor
106 *
107 * @param request
108 * @param portlet
109 * @param sc
110 */
111 WMCManagementPortletPerfom( HttpServletRequest request, Portlet portlet, ServletContext sc ) {
112 super( request, portlet, sc );
113 }
114
115 /**
116 * Saves the current context as a user context to a file in
117 * "WEB-INF/wmc/$USER_DIR/$mapPortletID$_CURRENTWMC.xml".
118 *
119 * @param userName
120 * @throws Exception
121 */
122 void doSavecontext( String userName )
123 throws Exception {
124
125 saveContext( userName, getMapPortletID() + "_" + AbstractPortletPerform.CURRENT_WMC );
126
127 }
128
129 /**
130 * Saves the current context as a user context to a file named by the user
131 *
132 * @param userName
133 * @throws Exception
134 */
135 void doSavenamedcontext( String userName )
136 throws Exception {
137
138 saveContext( userName, parameter.get( "CONTEXTNAME" ) );
139
140 }
141
142 /**
143 *
144 * @param userName
145 * @param cntxtName
146 * @throws PortalException
147 * @throws ParserConfigurationException
148 * @throws IOException
149 */
150 private void saveContext( String userName, String cntxtName )
151 throws PortalException, ParserConfigurationException, IOException {
152 LOG.logDebug( "Preparing context save." );
153 ViewContext vc = getCurrentViewContext( getInitParam( INIT_MAPPORTLETID ) );
154 if ( vc == null ) {
155 String msg = Messages.getMessage( "IGEO_PORTLET_NO_VC_AVAILABLE", getInitParam( INIT_MAPPORTLETID ) );
156 LOG.logDebug( msg );
157 throw new PortalException( msg );
158 }
159
160 File dir = new File( sc.getRealPath( "WEB-INF/wmc/" + userName ) );
161 LOG.logDebug( "Found directory '" + dir.getAbsolutePath() + "' for user '" + userName + "'." );
162
163 HttpSession ses = request.getSession( false );
164
165 LOG.logDebug( "Session is '" + ses + "'." );
166
167 // create user director if not exists
168 System.out.println( dir );
169 if ( !dir.exists() ) {
170 System.out.println( 555 );
171 dir.mkdir();
172 }
173
174 if ( ses != null ) {
175 storeContext( dir, cntxtName, vc );
176 }
177 }
178
179 /**
180 * Saves the current context as a shared context to a file in "WEB-INF/wmc/shared".
181 *
182 * @param userName
183 * @throws Exception
184 */
185 void doSaveshared( String userName )
186 throws Exception {
187
188 ViewContext vc = getCurrentViewContext( getInitParam( INIT_MAPPORTLETID ) );
189 if ( vc == null ) {
190 String msg = Messages.getMessage( "IGEO_PORTLET_NO_VC_AVAILABLE", getInitParam( INIT_MAPPORTLETID ) );
191 throw new PortalException( msg );
192 }
193
194 updateVC( userName, vc );
195 String filename = "wmc_" + String.valueOf( System.currentTimeMillis() );
196
197 File dir = new File( sc.getRealPath( "WEB-INF/wmc/shared/" ) );
198
199 HttpSession ses = request.getSession( false );
200 if ( ses != null && dir.exists() ) {
201 storeContext( dir, filename, vc );
202 }
203
204 sharedContextList.add( WMCMetadataSurrogate.createFromWMC( filename + ".xml", vc ) );
205 }
206
207 /**
208 * Updates the WMC using the info available inteh request. Currently title, abstract, keywords
209 * and user (author) are update. The first three come from the request parameters TITLE,
210 * ABSTRACT and KEYWORDS, and the user name come from the session.
211 *
212 * @param userName
213 * @param vc
214 * @throws ContextException
215 * @throws PortalException
216 */
217 private void updateVC( String userName, ViewContext vc )
218 throws ContextException, PortalException {
219
220 String title = parameter.get( TITLE );
221 String _abstract = parameter.get( DESCRIPTION );
222 String keywords = parameter.get( KEYWORDS );
223
224 General general = vc.getGeneral();
225 if ( userName != null && userName.length() > 0 ) {
226 general.getContactInformation().setIndividualName( new String[] { userName } );
227 }
228 String charset = WebUtils.readCharsetFromContentType( request );
229 try {
230 if ( title != null ) {
231 general.setTitle( URLDecoder.decode( title, charset ) );
232 }
233 if ( _abstract != null ) {
234 general.setAbstract( URLDecoder.decode( _abstract, charset ) );
235 }
236 if ( keywords != null ) {
237 keywords = URLDecoder.decode( keywords, charset );
238 String kwords[] = keywords.split( "," );
239 general.setKeywords( kwords );
240 }
241 } catch ( UnsupportedEncodingException e ) {
242 LOG.logError( e.getMessage(), e );
243 String msg = Messages.getMessage( "IGEO_PORTLET_CONEXTMNG_DECODING", charset );
244 throw new PortalException( msg, e );
245 }
246
247 }
248
249 /**
250 *
251 * @param dir
252 * @param name
253 * @param context
254 * @throws ParserConfigurationException
255 * @throws IOException
256 */
257 private void storeContext( File dir, String name, ViewContext context )
258 throws ParserConfigurationException, IOException {
259
260 XMLFragment xml = XMLFactory.export( context );
261
262 File file = new File( dir.getAbsolutePath() + '/' + name + ".xml" );
263
264 LOG.logDebug( "Saving context to '" + file.getAbsolutePath() + "'." );
265
266 String charset = WebUtils.readCharsetFromContentType( request );
267 OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream( file ), charset );
268 xml.write( osw );
269 osw.close();
270 }
271
272 /**
273 * Loads a context from a file pointed to by the request parameter "wmc_filename". The file must
274 * be in the "WEB-INF/wmc/shared/" directory.
275 *
276 * @throws Exception
277 */
278 void doLoadcontext()
279 throws Exception {
280
281 String wmcFilename = request.getParameter( "wmc_filename" );
282 File file = new File( sc.getRealPath( "WEB-INF/wmc/shared/" + wmcFilename ) );
283 loadContext( file );
284
285 }
286
287 /**
288 * Loads a context from a file pointed to by the request parameter "wmc_filename". The file must
289 * be in the "WEB-INF/wmc/$USER_NAME$/" directory.
290 *
291 * @param userName
292 * name of the user who loads a context
293 * @throws Exception
294 */
295 void doLoadnamedcontext( String userName )
296 throws Exception {
297
298 String wmcFilename = request.getParameter( "wmc_filename" );
299 File file = new File( sc.getRealPath( "WEB-INF/wmc/" + userName + '/' + wmcFilename ) );
300 loadContext( file );
301
302 }
303
304 /**
305 * loads a context
306 *
307 * @param file
308 * context file name
309 * @throws IOException
310 * @throws XMLParsingException
311 * @throws ContextException
312 * @throws SAXException
313 * @throws UnknownCRSException
314 * @throws MalformedURLException
315 */
316 private void loadContext( File file )
317 throws IOException, XMLParsingException, ContextException, SAXException,
318 UnknownCRSException, MalformedURLException {
319 User u = null;
320 String session = parameter.get( "PARAM_SESSIONID" );
321
322 String msg = StringTools.concat( 300, "Loading context from '", file.getAbsolutePath(), "' using user '", u,
323 "' and sessionID '", session, "'." );
324 LOG.logDebug( msg );
325
326 ViewContext vc = WebMapContextFactory.createViewContext( file.toURI().toURL(), u, session );
327
328 setCurrentMapContext( vc, getInitParam( INIT_MAPPORTLETID ) );
329 }
330
331 /**
332 *
333 * @return shared context list
334 */
335 public static List<WMCMetadataSurrogate> getSharedContextList() {
336 return sharedContextList;
337 }
338
339 @Override
340 public void buildNormalContext()
341 throws PortalException {
342 super.buildNormalContext();
343
344 // TODO
345 // remove this from buildNormalContext()
346 if ( sharedContextList.size() == 0 ) {
347 String s = sc.getRealPath( "WEB-INF/wmc/shared/" );
348 File directory = new File( s );
349
350 ConvenienceFileFilter cff = new ConvenienceFileFilter( false, "XML" );
351 File[] wmcFiles = directory.listFiles( cff );
352
353 for ( int i = 0; i < wmcFiles.length; i++ ) {
354 try {
355 XMLFragment xml = new XMLFragment( wmcFiles[i].toURI().toURL() );
356 Element root = xml.getRootElement();
357 String xpath = "cntxt:General/cntxt:ContactInformation/cntxt:ContactPersonPrimary/cntxt:ContactPerson[0]";
358 String author = XMLTools.getNodeAsString( root, xpath, nsContext, "-" );
359 xpath = "cntxt:General/cntxt:Title";
360 String title = XMLTools.getNodeAsString( root, xpath, nsContext, "-" );
361 String abstract_ = XMLTools.getNodeAsString( root, "cntxt:General/cntxt:Abstract", nsContext, "-" );
362 xpath = "cntxt:General/cntxt:KeywordList/cntxt:Keyword";
363 String[] keywords = XMLTools.getNodesAsStrings( root, xpath, nsContext );
364
365 WMCMetadataSurrogate wmcs = new WMCMetadataSurrogate( wmcFiles[i].getName(), author, title,
366 abstract_, keywords );
367 sharedContextList.add( wmcs );
368 } catch ( Exception e ) {
369 e.printStackTrace();
370 }
371 }
372 }
373 }
374
375 }