1 package org.sourceforge.vlibrary.user.actions;
2
3
4 import java.lang.reflect.InvocationTargetException;
5 import java.util.ArrayList;
6
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.apache.commons.beanutils.PropertyUtils;
11 import org.apache.log4j.Logger;
12 import org.apache.struts.action.ActionForm;
13 import org.apache.struts.action.ActionForward;
14 import org.apache.struts.action.ActionMapping;
15 import org.apache.struts.util.MessageResources;
16 import org.sourceforge.vlibrary.Constants;
17 import org.sourceforge.vlibrary.exceptions.LibraryException;
18 import org.sourceforge.vlibrary.user.domain.Reader;
19 import org.sourceforge.vlibrary.user.forms.ReaderForm;
20
21
22
23
24
25
26
27
28 public class SaveReaderAction extends LibraryAction {
29
30
31 private static Logger logger =
32 Logger.getLogger(SaveReaderAction.class.getName());
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public ActionForward executeAction(ActionMapping mapping,
47 ActionForm form,
48 HttpServletRequest request,
49 HttpServletResponse response,
50 MessageResources messages)
51 throws Exception {
52
53
54 ReaderForm frm = (ReaderForm) form;
55 String action = request.getParameter(Constants.ACTION);
56
57 if (action == null) {
58 action = Constants.CREATE;
59 }
60
61 ArrayList<LibraryException> errors = new ArrayList<LibraryException>();
62
63
64 Reader rd = new Reader();
65
66 try {
67 PropertyUtils.copyProperties(rd, frm);
68 } catch (InvocationTargetException e) {
69 Throwable t = e.getTargetException();
70 if (t == null) {
71 t = e;
72 }
73 String errString = messages.getMessage("error.reader.reflection");
74 logger.error(errString, t);
75 errors.add(new LibraryException(errString,t));
76 return standardForward(mapping,request,errors);
77 } catch (Throwable t) {
78 String errString = messages.getMessage("error.reader.populate");
79 logger.error(errString, t);
80 errors.add(new LibraryException(errString,t));
81 return standardForward(mapping,request,errors);
82 }
83
84
85 try {
86 if (action.equals("Create")) {
87 libraryManager.insertReader(rd);
88 } else {
89 if (action.equals("Update")) {
90 libraryManager.updateReader(rd);
91 } else {
92 String errString = messages.getMessage
93 ("error.action.invalid");
94 logger.error(errString);
95 errors.add(new LibraryException(errString));
96 }
97 }
98 } catch (Throwable t) {
99 String errString = messages.getMessage
100 ("error.reader.update");
101 logger.error(errString,t);
102 errors.add(new LibraryException(errString,t));
103 }
104
105 if (errors.isEmpty() && logger.isInfoEnabled()) {
106
107 if (action.equals("Create")) {
108 logger.info(messages.getMessage("reader.create.successful",
109 rd.getFirstName() + " " + rd.getLastName()));
110 } else {
111 logger.info(messages.getMessage("reader.update.successful",
112 rd.getFirstName() + " " + rd.getLastName()));
113 }
114 }
115 return standardForward(mapping,request,errors);
116 }
117 }