1 package org.sourceforge.vlibrary.user.actions;
2
3 import java.util.ArrayList;
4
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7
8 import org.apache.commons.beanutils.PropertyUtils;
9 import org.apache.log4j.Logger;
10 import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionForward;
12 import org.apache.struts.action.ActionMapping;
13 import org.apache.struts.util.MessageResources;
14 import org.sourceforge.vlibrary.Constants;
15 import org.sourceforge.vlibrary.exceptions.LibraryException;
16 import org.sourceforge.vlibrary.user.domain.Book;
17 import org.sourceforge.vlibrary.user.forms.BookForm;
18 import org.sourceforge.vlibrary.user.valuebeans.BookEditTransaction;
19
20
21
22
23
24
25
26
27 public class SaveBookAction extends LibraryAction {
28
29 private static Logger logger =
30 Logger.getLogger(SaveBookAction.class.getName());
31
32
33
34
35
36
37
38
39
40
41
42
43 @Override
44 public ActionForward executeAction(ActionMapping mapping,
45 ActionForm form,
46 HttpServletRequest request,
47 HttpServletResponse response,
48 MessageResources messages)
49 throws Exception {
50
51 final ArrayList<LibraryException> errors = new ArrayList<LibraryException>();
52
53 final BookForm frm = (BookForm) form;
54
55 String action = request.getParameter(Constants.ACTION);
56
57 if (action == null) {
58 action = Constants.CREATE;
59 }
60
61 Book bk = new Book();
62
63
64 try {
65 libraryManager.createAuthorIds(frm.getAuthors());
66 libraryManager.createSubjectIds(frm.getSubjects());
67 } catch (final Throwable t) {
68 final String errString = messages.getMessage("error.book.properties");
69 logger.error(errString,t);
70 errors.add(new LibraryException(errString,t));
71 return standardForward(mapping,request,errors);
72 }
73
74 if (action.equals(Constants.UPDATE)) {
75
76 try {
77 bk.setId(frm.getId());
78 PropertyUtils.copyProperties(bk, frm);
79 } catch (final NoSuchMethodException e) {
80
81 logger.debug("Form Bean has more properties, exception ignored:" + e.getMessage());
82 } catch (final Throwable t) {
83 final String errString = messages.getMessage("error.book.properties");
84 logger.error(errString,t);
85 errors.add(new LibraryException(errString,t));
86 return standardForward(mapping,request,errors);
87 }
88 }
89
90
91 if (action.equals(Constants.CREATE)) {
92 try {
93 PropertyUtils.copyProperties(bk, frm);
94
95 bk = libraryManager.createBook(bk, frm.getSubjects(), frm.getAuthors());
96
97 frm.setId(bk.getId());
98 } catch (final Throwable t) {
99 final String errString = messages.getMessage("error.book.insert");
100 logger.error(errString,t);
101 errors.add(new LibraryException(errString,t));
102 return standardForward(mapping,request,errors);
103 }
104 }
105
106
107 try {
108 libraryManager.updateBook(bk );
109 } catch (final Throwable t) {
110 final String errString = messages.getMessage("error.book.update");
111 logger.error(errString,t);
112 errors.add(new LibraryException(errString,t));
113 }
114
115
116
117 final BookEditTransaction t = new BookEditTransaction();
118 t.setTitle(bk.getTitle());
119 t.setId(bk.getId());
120 t.setAction(action);
121 request.setAttribute(Constants.BOOK,t);
122 bk = null;
123
124 return standardForward(mapping,request,errors);
125 }
126 }