View Javadoc

1   package net.sf.cantina.system;
2   
3   import net.sf.cantina.DataSource;
4   import net.sf.cantina.Document;
5   import net.sf.cantina.webgui.StrutsAction;
6   import net.sf.cantina.exceptions.ObjectNotFoundException;
7   import net.sf.cantina.exceptions.InvalidXmlException;
8   import net.sf.cantina.exceptions.ForbiddenOperationException;
9   import net.sf.cantina.util.Localizer;
10  import org.apache.log4j.Logger;
11  import org.apache.struts.action.*;
12  import org.apache.struts.upload.FormFile;
13  import org.apache.struts.validator.DynaValidatorActionForm;
14  
15  import javax.servlet.http.HttpServletRequest;
16  import javax.servlet.http.HttpServletResponse;
17  import java.util.*;
18  
19  /***
20   * @author Stephane JAIS
21   */
22  public class ChangeDocumentAction
23    extends StrutsAction
24  {
25    private static final Logger logger = Logger.getLogger(ChangeDocumentAction.class);
26  
27    public ActionForward execute(ActionMapping mapping,
28                                 ActionForm form,
29                                 HttpServletRequest request,
30                                 HttpServletResponse response)
31      throws Exception
32    {
33  
34      ActionMessages messages = new ActionMessages();
35      DynaValidatorActionForm dynaform = (DynaValidatorActionForm)form;
36      String documentId = (String)dynaform.get("documentId");
37      byte[] content;
38  
39      //file upload if binContent is not null
40      if (null != dynaform.get("binContent"))
41        content = ((FormFile)dynaform.get("binContent")).getFileData();
42      else
43      {
44        logger.debug("Parsing content in charset ["+request.getCharacterEncoding()+"]");
45        content = ((String)dynaform.get("content")).getBytes(request.getCharacterEncoding());
46      }
47  
48      Locale locale = Localizer.parseLocaleString((String)dynaform.get("locale"));
49  
50      ActionErrors errors = new ActionErrors();
51  
52      Document document = DataSource.getInstance().loadDocument(documentId);
53      try
54      {
55        Transaction t = new ChangeDocumentTransaction(
56          documentId,
57          new HttpUser(request),
58          content,
59          locale
60        );
61        t.execute();
62        saveInRequest(request,document);
63        messages.add("content",new ActionMessage("document.updated",documentId));
64        saveMessages(request,messages);
65      } catch (ObjectNotFoundException e)
66      {
67        errors.add("documentId",new ActionError("document.not.found",documentId));
68        saveErrors(request,errors);
69        return mapping.findForward(MAPPING_INPUT);
70      } catch (InvalidXmlException e)
71      {
72        for (int i = 0; i < e.getErrors().length; i++)
73        {
74          int line = e.getErrors()[i].getLineNumber();
75          int column = e.getErrors()[i].getColumnNumber();
76          StringBuffer extract = new StringBuffer(e.getErrors()[i].getLocalizedMessage());
77          extract.append("\n");
78          String xmlContent = new String(content, "utf-8");
79          //get the lines, adding an empty one when xmlContent ends with \n
80          List lines = new Vector(Arrays.asList(xmlContent.split("\n")));
81          if (xmlContent.endsWith("\n"))
82            lines.add("");
83          //add relevant line
84          extract.append(lines.get(line - 1));
85          extract.append("\n");
86          //construct the arrow
87          for (int j=0; j<column-2; j++)
88            extract.append("-");
89          extract.append("^");
90          //add to errors
91          errors.add("content",new ActionError("invalid.xml",
92            ""+line,
93            ""+column,extract));
94        }
95        saveErrors(request,errors);
96        saveInRequest(request,document);
97        return mapping.findForward(MAPPING_INPUT);
98      } catch (ForbiddenOperationException e) {
99        errors.add("documentId",new ActionError("document.edition.forbidden",documentId,document.getRealm().getName()));
100       saveErrors(request,errors);
101       saveInRequest(request,document);
102       return mapping.findForward(MAPPING_INPUT);
103     }
104 
105     return mapping.findForward(MAPPING_SUCCESS);
106   }
107 }
108