001 /*
002 *
003 * $Revision: 15202 $ $Date: 2009-05-15 17:00:44 +0200 (Fri, 15 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 static org.jdom.Namespace.XML_NAMESPACE;
027 import static org.mycore.common.MCRConstants.XLINK_NAMESPACE;
028 import static org.mycore.common.MCRConstants.XSI_NAMESPACE;
029
030 import java.io.File;
031 import java.util.List;
032
033 import org.apache.log4j.Logger;
034 import org.jdom.Document;
035 import org.jdom.Element;
036 import org.jdom.xpath.XPath;
037 import org.mycore.access.MCRAccessManager;
038 import org.mycore.common.MCRConfiguration;
039 import org.mycore.common.MCRMailer;
040 import org.mycore.common.MCRUtils;
041 import org.mycore.datamodel.common.MCRActiveLinkException;
042 import org.mycore.datamodel.metadata.MCRDerivate;
043 import org.mycore.datamodel.metadata.MCRObjectID;
044 import org.mycore.frontend.editor.MCREditorSubmission;
045
046 /**
047 * The servlet store the MCREditorServlet output XML in a file of a MCR type
048 * dependencies directory, check it dependence of the MCR type and store the XML
049 * in a file in this directory or if an error was occured start the editor again
050 * with <b>todo </b> <em>repair</em>.
051 *
052 * @author Jens Kupferschmidt
053 * @version $Revision: 15202 $ $Date: 2009-05-15 17:00:44 +0200 (Fri, 15 May 2009) $
054 */
055 public class MCRCheckEditDerivateServlet extends MCRCheckBase {
056
057 private static final long serialVersionUID = 1L;
058 private static Logger LOGGER = Logger.getLogger(MCRCheckEditDerivateServlet.class);
059
060 /**
061 * This method overrides doGetPost of MCRServlet. <br />
062 */
063 public void doGetPost(MCRServletJob job) throws Exception {
064 // read the XML data
065 MCREditorSubmission sub = (MCREditorSubmission) (job.getRequest().getAttribute("MCREditorSubmission"));
066 Document indoc = sub.getXML();
067 if (LOGGER.isDebugEnabled()) {
068 MCRUtils.writeJDOMToSysout(indoc);
069 }
070
071 // create a metadata object and prepare it
072 MCRObjectID derID = null;
073 boolean okay = false;
074 String url = getNextURL(derID, okay);
075 try {
076 Element root = indoc.getRootElement();
077 derID = new MCRObjectID(root.getAttributeValue("ID"));
078 root.setAttribute("noNamespaceSchemaLocation", "datamodel-derivate.xsd", XSI_NAMESPACE);
079 root.addNamespaceDeclaration(XLINK_NAMESPACE);
080 root.addNamespaceDeclaration(XSI_NAMESPACE);
081 XPath titles = XPath.newInstance("/mycorederivate/derivate/titles/title");
082 for (Object node : titles.selectNodes(indoc)) {
083 Element e = (Element) node;
084 if (e.getAttribute("lang") != null) {
085 e.getAttribute("lang").setNamespace(XML_NAMESPACE);
086 }
087 }
088 XPath linkmetas = XPath.newInstance("/mycorederivate/derivate/linkmetas/linkmeta");
089 for (Object node : linkmetas.selectNodes(indoc)) {
090 Element e = (Element) node;
091 if (e.getAttribute("href") != null) {
092 e.getAttribute("href").setNamespace(XLINK_NAMESPACE);
093 }
094 if (e.getAttribute("title") != null) {
095 e.getAttribute("title").setNamespace(XLINK_NAMESPACE);
096 }
097 if (e.getAttribute("type") != null) {
098 e.getAttribute("type").setNamespace(XLINK_NAMESPACE);
099 }
100 }
101 if (LOGGER.isDebugEnabled()) {
102 MCRUtils.writeJDOMToSysout(indoc);
103 }
104 byte[] xml = MCRUtils.getByteArray(indoc);
105
106 // read data
107 Element e = (Element) linkmetas.selectSingleNode(indoc);
108 MCRDerivate der = new MCRDerivate();
109 der.setFromXML(xml, true);
110 MCRObjectID objID = new MCRObjectID(e.getAttributeValue("href", XLINK_NAMESPACE));
111
112 // check access
113 if (!checkAccess(objID)) {
114 job.getResponse().sendRedirect(getBaseURL() + usererrorpage);
115 return;
116 }
117
118 // update data
119 StringBuffer sb = new StringBuffer();
120 sb.append(WFM.getDirectoryPath(objID.getBase())).append(File.separator).append(derID.getId()).append(".xml");
121 MCRUtils.writeJDOMToFile(indoc, new File(sb.toString()));
122 okay = true;
123 url = getNextURL(objID, okay);
124 sendMail(derID);
125 } catch (Exception e) {
126 e.printStackTrace();
127 }
128 // return
129 job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(getBaseURL() + url));
130 }
131
132 /**
133 * The method is a dummy and return an URL with the next working step.
134 *
135 * @param ID
136 * the MCRObjectID of the MCRObject
137 * @param okay
138 * the return value of the store operation
139 * @return the next URL as String
140 */
141 public final String getNextURL(MCRObjectID ID, boolean okay) throws MCRActiveLinkException {
142 StringBuffer sb = new StringBuffer();
143 if (okay) {
144 // return all is ready
145 sb.append(WFM.getWorkflowFile(pagedir, ID.getBase()));
146 } else {
147 sb.append(pagedir).append(MCRConfiguration.instance().getString("MCR.SWF.PageErrorStore", "editor_error_store.xml"));
148 }
149 return sb.toString();
150 }
151
152 /**
153 * The method send a message to the mail address for the MCRObjectType.
154 *
155 * @param ID
156 * the MCRObjectID of the MCRObject
157 */
158 public final void sendMail(MCRObjectID ID) {
159 List<String> addr = WFM.getMailAddress(ID.getTypeId(), "weditder");
160 if (addr.size() == 0) {
161 return;
162 }
163 String sender = WFM.getMailSender();
164 String appl = MCRConfiguration.instance().getString("MCR.SWF.Mail.ApplicationID", "MyCoRe");
165 String subject = "Automatically generated message from " + appl;
166 StringBuffer text = new StringBuffer();
167 text.append("The title of the derivate with the ID ").append(ID.getId()).append(" was changed in the workflow.");
168 LOGGER.info(text.toString());
169 try {
170 MCRMailer.send(sender, addr, subject, text.toString(), false);
171 } catch (Exception ex) {
172 LOGGER.error("Can't send a mail to " + addr);
173 }
174 }
175
176 /**
177 * check the access permission
178 * @param ID the mycore ID
179 * @return true if the access is set
180 */
181 protected boolean checkAccess(MCRObjectID ID) {
182 if (MCRAccessManager.checkPermission("create-"+ID.getBase())) {
183 return true;
184 }
185 if (MCRAccessManager.checkPermission("create-"+ID.getTypeId())) {
186 return true;
187 }
188 return false;
189 }
190
191 }