Index: PsmlUpdateAction.java =================================================================== RCS file: /home/cvspublic/jakarta-jetspeed/src/java/org/apache/jetspeed/modules/actions/portlets/PsmlUpdateAction.java,v retrieving revision 1.5 diff -u -r1.5 PsmlUpdateAction.java --- PsmlUpdateAction.java 26 Jul 2002 01:47:20 -0000 1.5 +++ PsmlUpdateAction.java 9 Aug 2002 21:50:45 -0000 @@ -60,6 +60,9 @@ import org.apache.turbine.util.RunData; import org.apache.turbine.util.StringUtils; import org.apache.turbine.util.security.EntityExistsException; +import org.apache.turbine.services.TurbineServices; +import org.apache.turbine.services.servlet.TurbineServlet; +import org.apache.turbine.services.resources.ResourceService; // Velocity Stuff import org.apache.velocity.context.Context; @@ -67,6 +70,10 @@ //Java import java.util.Vector; import java.util.Iterator; +import java.io.File; +import java.io.FileWriter; +import java.io.FileReader; +import java.io.IOException; //Jetspeed import org.apache.jetspeed.modules.actions.portlets.security.SecurityConstants; @@ -85,6 +92,21 @@ import org.apache.jetspeed.util.template.JetspeedLink; import org.apache.jetspeed.util.template.JetspeedLinkFactory; import org.apache.jetspeed.services.resources.JetspeedResources; +import org.apache.jetspeed.services.psmlmanager.PsmlManagerService; + +//castor support +import org.exolab.castor.xml.MarshalException; +import org.exolab.castor.xml.Unmarshaller; +import org.exolab.castor.xml.Marshaller; +import org.exolab.castor.xml.ValidationException; +import org.exolab.castor.mapping.Mapping; +import org.exolab.castor.mapping.MappingException; +import org.xml.sax.InputSource; + +// serialization support +import org.apache.xml.serialize.Serializer; +import org.apache.xml.serialize.XMLSerializer; +import org.apache.xml.serialize.OutputFormat; /** * This action enables to update the psml entries @@ -101,6 +123,7 @@ protected static final String CATEGORY_NAME = "categoryName"; protected static final String CATEGORY_VALUE = "categoryValue"; protected static final String COPY_FROM = "copyFrom"; + protected static final String COPY_TO = "copyTo"; protected static final String TEMP_LOCATOR = "tempLocator"; protected static final String PSML_UPDATE_PANE = "PsmlForm"; /** @@ -206,6 +229,57 @@ context.put(COPY_FROM, rundata.getUser().getTemp(COPY_FROM)); } } + + if(mode != null && mode.equals("export")) + { + if (msgid == null) + { + if(path == null) + { + context.put(COPY_TO, "c:/temp"); + context.put(COPY_FROM, "user/anon/media-type/html/page/default.psml"); + } + else + { + ProfileLocator tmpLocator = new BaseProfileLocator(); + tmpLocator.createFromPath(path); + Profile profile = Profiler.getProfile(tmpLocator); + if (profile != null) + { + rundata.getUser().setTemp(TEMP_LOCATOR, tmpLocator); + context.put("profile", profile); + } + + String categoryName = "group"; + String categoryValue = tmpLocator.getGroupName(); + if (categoryValue == null) + { + categoryName = "role"; + categoryValue = tmpLocator.getRoleName(); + if (categoryValue == null) + { + categoryName = "user"; + categoryValue = tmpLocator.getUserName(); + if (categoryValue == null) + { + categoryName = "user"; + categoryValue = "anon"; + } + } + + } + + context.put(COPY_TO, "c:/temp/"+categoryValue+".psml"); + context.put(COPY_FROM, path); + } + } + else + { + context.put(COPY_TO, rundata.getUser().getTemp(COPY_TO)); + context.put(COPY_FROM, rundata.getUser().getTemp(COPY_FROM)); + } + } + } catch (Exception e) { @@ -424,6 +498,197 @@ { rundata.getUser().setTemp(PSML_REFRESH_FLAG, TRUE); } + + /** + * File Export Action for Psml. + * + * @param rundata The turbine rundata context for this request. + * @param context The velocity context for this request. + */ + public void doExport(RunData rundata, Context context) + throws Exception + { + Profile profile = null; + ProfileLocator locator = null; + String copyTo = null; + String copyFrom = null; + + try + { + copyFrom = rundata.getParameters().getString("CopyFrom"); + copyTo = rundata.getParameters().getString("CopyTo"); + + //TODO: check if file already exists + /*if (name == null || name.trim().length() == 0) + { + JetspeedLink link = JetspeedLinkFactory.getInstance(rundata); + DynamicURI duri = link.getPaneByName(PSML_UPDATE_PANE) + .addPathInfo(SecurityConstants.PARAM_MODE, + SecurityConstants.PARAM_MODE_INSERT) + .addPathInfo(SecurityConstants.PARAM_MSGID, + SecurityConstants.MID_INVALID_ENTITY_NAME); + JetspeedLinkFactory.putInstance(link); + rundata.setRedirectURI(duri.toString()); + + //save user entered values + if (copyTo != null) + rundata.getUser().setTemp(COPY_TO, copyTo); + if (copyFrom != null) + rundata.getUser().setTemp(COPY_FROM, copyFrom); + return; + } */ + + // + // retrieve the profile to clone + // + ProfileLocator baseLocator = new BaseProfileLocator(); + baseLocator.createFromPath(copyFrom); + Profile baseProfile = Profiler.getProfile(baseLocator); + + // + // Export profile + // + if(baseProfile != null) + { + PSMLDocument doc = baseProfile.getDocument(); + if(doc != null) + { + this.saveDocument(copyTo,doc); + rundata.setMessage("Profile ["+copyFrom+"] has been saved to disk in ["+copyTo+"]"); + } + } + else + { + Log.error("Profile listed in Copy From Not Found!"); + } + } + catch (Exception e) + { + // log the error msg + Log.error(e); + + // + // dup key found - display error message - bring back to same screen + // + JetspeedLink link = JetspeedLinkFactory.getInstance(rundata); + DynamicURI duri = link.getPaneByName(PSML_UPDATE_PANE) + .addPathInfo(SecurityConstants.PARAM_MODE, + "export") + .addPathInfo(SecurityConstants.PARAM_MSGID, + SecurityConstants.MID_UPDATE_FAILED); + JetspeedLinkFactory.putInstance(link); + rundata.setRedirectURI(duri.toString()); + } + // save values that user just entered so they don't have to re-enter + if (copyTo != null) + rundata.getUser().setTemp(COPY_TO, copyFrom); + if (copyFrom != null) + rundata.getUser().setTemp(COPY_FROM, copyFrom); + + } + + /** Save the PSML document on disk to the specififed fileOrUrl + * + * @param fileOrUrl a String representing either an absolute URL + * or an absolute filepath + * @param doc the document to save + */ + private boolean saveDocument(String fileOrUrl, PSMLDocument doc) + { + boolean success = false; + + if (doc == null) return false; + File f = new File(fileOrUrl); + + FileWriter writer = null; + + try + { + writer = new FileWriter(f); + // create the serializer output format + OutputFormat format = new OutputFormat(); + format.setIndenting(true); + format.setIndent(4); + Serializer serializer = new XMLSerializer(writer,format); + Marshaller marshaller = new Marshaller(serializer.asDocumentHandler()); + marshaller.setMapping(this.loadMapping()); + marshaller.marshal(doc.getPortlets()); + + success = true; + } + catch (MarshalException e) + { + Log.error("PSMLManager: Could not marshal the file "+f.getAbsolutePath(), e); + } + catch (MappingException e) + { + Log.error("PSMLManager: Could not marshal the file "+f.getAbsolutePath(), e); + } + catch (ValidationException e) + { + Log.error("PSMLManager: document "+f.getAbsolutePath()+" is not valid", e); + } + catch (IOException e) + { + Log.error("PSMLManager: Could not save the file "+f.getAbsolutePath(), e); + } + catch (Exception e) + { + Log.error("PSMLManager: Error while saving "+f.getAbsolutePath(), e); + } + finally + { + try { writer.close(); } catch (IOException e) {} + } + + return success; + } + + /** + * Loads psml mapping file + * + * @exception Exception + */ + private Mapping loadMapping() + throws Exception + { + // get configuration parameters from Jetspeed Resources + ResourceService serviceConf = ((TurbineServices)TurbineServices.getInstance()) + .getResources(PsmlManagerService.SERVICE_NAME); + + // test the mapping file and create the mapping object + Mapping mapping = null; + String mapFile = serviceConf.getString("mapping","${webappRoot}/WEB-INF/conf/psml-mapping.xml"); + mapFile = TurbineServlet.getRealPath( mapFile ); + if (mapFile != null) + { + File map = new File(mapFile); + Log.debug("Loading psml mapping file "+mapFile); + if (map.exists() && map.isFile() && map.canRead()) + { + try + { + mapping = new Mapping(); + InputSource is = new InputSource( new FileReader(map) ); + is.setSystemId( mapFile ); + mapping.loadMapping( is ); + } + catch (Exception e) + { + Log.error("Error in psml mapping creation",e); + throw new Exception("Error in mapping"); + } + } + else + { + throw new Exception("PSML Mapping not found or not a file or unreadable: "+mapFile); + } + } + + return mapping; + } + + }