psmlmanageraction.java

来自「jetspeed源代码」· Java 代码 · 共 1,653 行 · 第 1/5 页

JAVA
1,653
字号
        {
            rundata.getUser().removeTemp(CACHED_PSML);
        }
    }

    /**
     * 
     * @param rundata
     * @return 
     */
    private boolean getRefreshPsmlFlag(RunData rundata)
    {
        return (rundata.getUser().getTemp(PSML_REFRESH_FLAG, FALSE)).equals(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");

            //
            // retrieve the profile to clone
            //
            ProfileLocator baseLocator = Profiler.createLocator();
            baseLocator.createFromPath(copyFrom);
            Profile baseProfile = Profiler.getProfile(baseLocator);

            //
            // Export profile
            //
            if (baseProfile != null)
            {
                PSMLDocument doc = baseProfile.getDocument();
                if (doc != null)
                {
                    if (!this.saveDocument(copyTo,doc))
                        throw new Exception("Failed to save PSML document");
                    rundata.addMessage("Profile [" + copyFrom + "] has been saved to disk in [" + copyTo + "]<br>");
                }
            }
            else
            {
                logger.error("Profile listed in Copy From Not Found!");
            }
        }
        catch (Exception e)
        {
            // log the error msg
            logger.error("Exception", e);

            //
            // dup key found - display error message - bring back to same screen
            //
            JetspeedLink link = JetspeedLinkFactory.getInstance(rundata);
            DynamicURI duri = link.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, copyTo);
        if (copyFrom != null)
            rundata.getUser().setTemp(COPY_FROM, copyFrom);

    }

    /**
     * File Export All Action for Psml.
     *
     * @param rundata The turbine rundata context for this request.
     * @param context The velocity context for this request.
     */
    public void doExportall(RunData rundata, Context context)
    throws Exception
    {
        String copyTo = null;

        logger.info("PsmlUpdateAction: Starting export all operation");

        try
        {
            copyTo = rundata.getParameters().getString("CopyTo");

            //
            // retrieve the profiles to export
            //
            Iterator i = Profiler.query(new QueryLocator(QueryLocator.QUERY_ALL));
            while (i.hasNext())
            {
                Profile profile = (Profile) i.next();
                PSMLDocument doc = profile.getDocument();
                if (doc != null)
                {
                    // Build the fully qualified file name
                    StringBuffer copyToFile = new StringBuffer(copyTo);
                    copyToFile.append(File.separator);
                    if (profile.getGroupName() != null)
                    {
                        copyToFile.append("group");
                        copyToFile.append(File.separator);
                        copyToFile.append(profile.getGroupName());
                        copyToFile.append(File.separator);
                    }
                    else if (profile.getRoleName() != null)
                    {
                        copyToFile.append("role");
                        copyToFile.append(File.separator);
                        copyToFile.append(profile.getRoleName());
                        copyToFile.append(File.separator);
                    }
                    else if (profile.getUserName() != null)
                    {
                        copyToFile.append("user");
                        copyToFile.append(File.separator);
                        copyToFile.append(profile.getUserName());
                        copyToFile.append(File.separator);
                    }
                    if (profile.getMediaType() != null)
                    {
                        copyToFile.append(profile.getMediaType());
                        copyToFile.append(File.separator);
                    }
                    if (profile.getLanguage() != null)
                    {
                        copyToFile.append(profile.getLanguage());
                        copyToFile.append(File.separator);
                    }
                    if (profile.getCountry() != null)
                    {
                        copyToFile.append(profile.getCountry());
                        copyToFile.append(File.separator);
                    }
                    copyToFile.append(profile.getName());

                    if (!this.saveDocument(copyToFile.toString(), doc))
                    {
                        logger.error("Failed to save PSML document for [" + profile.getPath());
                    }
                    else
                    {
                        String msg = "Profile [" + profile.getPath() + "] has been saved to disk in [" + copyToFile.toString() + "]<br>";
                        logger.info(msg);
                        rundata.addMessage(msg);
                    }
                }
            }

        }
        catch (Exception e)
        {
            // log the error msg
            logger.error("Exception", e);

            //
            // dup key found - display error message - bring back to same screen
            //
            JetspeedLink link = JetspeedLinkFactory.getInstance(rundata);
            DynamicURI duri = link.addPathInfo(SecurityConstants.PARAM_MODE,
                                               "export_all")
                              .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, copyTo);
        }

        logger.info("PsmlUpdateAction: Ending export all operation");
    }

    /**
     * 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
     * @return 
     */
    private boolean saveDocument(String fileOrUrl, PSMLDocument doc)
    {
        boolean success = false;

        if (doc == null) return false;
        File f = new File(fileOrUrl);
        File d = new File(f.getParent());
        d.mkdirs();

        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)
        {
            logger.error("PsmlManagerAction: Could not marshal the file " + f.getAbsolutePath(), e);
        }
        catch (MappingException e)
        {
            logger.error("PsmlManagerAction: Could not marshal the file " + f.getAbsolutePath(), e);
        }
        catch (ValidationException e)
        {
            logger.error("PsmlManagerAction: document "+f.getAbsolutePath() + " is not valid", e);
        }
        catch (IOException e)
        {
            logger.error("PsmlManagerAction: Could not save the file " + f.getAbsolutePath(), e);
        }
        catch (Exception e)
        {
            logger.error("PsmlManagerAction: 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);
            if (logger.isDebugEnabled())
            {
                logger.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)
                {
                    logger.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;
    }

    /**
     * File Import Action for Psml.
     *
     * TODO: Implement file upload.
     *
     * @param rundata The turbine rundata context for this request.
     * @param context The velocity context for this request.
     */
    public void doImport(RunData rundata, Context context)
    throws Exception
    {
        Profile profile = null;
        ProfileLocator locator = null;
        String categoryName = null;
        String categoryValue = null;
        String copyFrom = null;
        String name = null;

        try
        {
            categoryName = rundata.getParameters().getString("CategoryName");
            categoryValue = rundata.getParameters().getString("CategoryValue");
            copyFrom = rundata.getParameters().getString("CopyFrom");
            name = rundata.getParameters().getString("name");
            //
            //create a new locator and set its values according to users input
            //

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?