001    /*
002     * 
003     * $Revision: 15272 $ $Date: 2009-05-26 12:37:41 +0200 (Tue, 26 May 2009) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.frontend.servlets;
025    
026    import java.io.File;
027    import java.io.FileOutputStream;
028    import java.io.IOException;
029    import java.util.List;
030    
031    import org.apache.log4j.Logger;
032    import org.mycore.access.MCRAccessManager;
033    import org.mycore.common.MCRConfiguration;
034    import org.mycore.common.MCRMailer;
035    import org.mycore.common.MCRUtils;
036    import org.mycore.datamodel.common.MCRActiveLinkException;
037    import org.mycore.datamodel.metadata.MCRObject;
038    import org.mycore.datamodel.metadata.MCRObjectID;
039    import org.mycore.datamodel.metadata.MCRObjectService;
040    
041    /**
042     * The servlet store the MCREditorServlet output XML in a file of a MCR type
043     * dependencies directory, check it dependence of the MCR type and store the XML
044     * in a file in this directory or if an error was occured start the editor again
045     * with <b>todo </b> <em>repair</em>.
046     * 
047     * @author Jens Kupferschmidt
048     * @version $Revision: 15272 $ $Date: 2009-05-26 12:37:41 +0200 (Tue, 26 May 2009) $
049     */
050    public class MCRCheckEditACLServlet extends MCRCheckACLBase {
051    
052        private static final long serialVersionUID = 1L;
053    
054        private static Logger LOGGER = Logger.getLogger(MCRCheckEditACLServlet.class);
055    
056        /**
057         * The method return an URL with the next working step. If okay flag is
058         * true, the object will present else it shows the error page.
059         * 
060         * @param ID
061         *            the MCRObjectID of the MCRObject
062         * @param okay
063         *            the return value of the store operation
064         * @return the next URL as String
065         */
066        protected String getNextURL(MCRObjectID ID, boolean okay) throws MCRActiveLinkException {
067            StringBuffer sb = new StringBuffer();
068            if (okay) {
069                sb.append(WFM.getWorkflowFile(pagedir, ID.getBase()));
070            } else {
071    
072                sb.append(pagedir).append(MCRConfiguration.instance().getString("MCR.SWF.PageErrorStore", "editor_error_store.xml"));
073            }
074            return sb.toString();
075        }
076    
077        /**
078         * The method send a message to the mail address for the MCRObjectType.
079         * 
080         * @param ID
081         *            the MCRObjectID of the MCRObject
082         */
083        public final void sendMail(MCRObjectID ID) {
084            List<String> addr = WFM.getMailAddress(ID.getBase(), "weditacl");
085    
086            if (addr.size() == 0) {
087                return;
088            }
089    
090            String sender = WFM.getMailSender();
091            String appl = MCRConfiguration.instance().getString("MCR.SWF.Mail.ApplicationID", "DocPortal");
092            String subject = "Automatically generated message from " + appl;
093            StringBuffer text = new StringBuffer();
094            text.append("The ACL data of the MyCoRe object of type ").append(ID.getTypeId()).append(" with the ID ").append(ID.getId()).append(
095                    " in the workflow was changes.");
096            LOGGER.info(text.toString());
097    
098            try {
099                MCRMailer.send(sender, addr, subject, text.toString(), false);
100            } catch (Exception ex) {
101                LOGGER.error("Can't send a mail to " + addr);
102            }
103        }
104    
105        /**
106         * The method store the incoming service data from the ACL editor to the
107         * workflow.
108         * 
109         * @param outelm
110         *            the service subelement of an MCRObject
111         * @param job
112         *            the MCRServletJob instance
113         * @param ID
114         *            the MCRObjectID
115         */
116        public final boolean storeService(org.jdom.Element outelm, MCRServletJob job, MCRObjectID ID) {
117            File impex = new File(WFM.getDirectoryPath(ID.getBase()), ID.getId() + ".xml");
118            MCRObject obj = new MCRObject();
119            obj.setFromURI(impex.toURI());
120            MCRObjectService service = new MCRObjectService();
121            service.setFromDOM(outelm);
122            obj.setService(service);
123    
124            // Save the prepared MCRObject/MCRDerivate to a file
125            FileOutputStream out = null;
126            try {
127                out = new FileOutputStream(impex);
128                out.write(MCRUtils.getByteArray(obj.createXML()));
129                out.flush();
130            } catch (IOException ex) {
131                LOGGER.error(ex.getMessage());
132                LOGGER.error("Exception while store to file " + impex);
133                try {
134                    errorHandlerIO(job);
135                } catch (Exception ioe) {
136                    ioe.printStackTrace();
137                }
138    
139                return false;
140            } finally {
141                if (out != null)
142                    try {
143                        out.close();
144                    } catch (IOException e) {
145                        e.printStackTrace();
146                    }
147            }
148    
149            LOGGER.info("Object " + ID.getId() + " stored under " + impex + ".");
150            return true;
151        }
152    
153        /**
154         * check the access permission
155         * @param ID the mycore ID
156         * @return true if the access is set
157         */
158        protected boolean checkAccess(MCRObjectID ID) {
159            if (MCRAccessManager.checkPermission("create-" + ID.getBase())) {
160                return true;
161            }
162            if (MCRAccessManager.checkPermission("create-" + ID.getTypeId())) {
163                return true;
164            }
165            return false;
166        }
167    
168    }