1 package org.sourceforge.vlibrary.user.actions;
2
3 import java.util.Map;
4 import java.util.ArrayList;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import org.apache.struts.action.ActionForm;
10 import org.apache.struts.action.ActionForward;
11 import org.apache.struts.action.ActionMapping;
12 import org.apache.struts.util.MessageResources;
13
14 import org.apache.commons.beanutils.PropertyUtils;
15 import org.apache.commons.lang.StringUtils;
16
17 import org.apache.log4j.Logger;
18
19 import org.sourceforge.vlibrary.Constants;
20 import org.sourceforge.vlibrary.user.domain.Book;
21 import org.sourceforge.vlibrary.user.domain.Reader;
22 import org.sourceforge.vlibrary.user.forms.BookForm;
23 import org.sourceforge.vlibrary.exceptions.LibraryException;
24
25 import org.sourceforge.vlibrary.user.domain.Author;
26 import org.sourceforge.vlibrary.user.domain.Subject;
27
28 import org.sourceforge.vlibrary.util.Utils;
29
30
31
32
33
34
35 public class EditBookAction extends LibraryAction {
36
37
38 private static Logger logger =
39 Logger.getLogger(EditBookAction.class.getName());
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public ActionForward executeAction(ActionMapping mapping,
60 ActionForm form,
61 HttpServletRequest request,
62 HttpServletResponse response,
63 MessageResources messages)
64 throws Exception {
65 ArrayList errors = new ArrayList();
66 String action = request.getParameter(Constants.ACTION);
67 String isbn = null;
68
69
70
71
72
73
74
75 if (action == null) {
76 Object queryString = request.getParameterMap().get(Constants.ISBN);
77 if ( queryString!= null &&
78 queryString instanceof String[] &&
79 ((String[])queryString).length > 0) {
80 action = Constants.PREFILLBOOKDETAILS;
81
82 isbn = ((String[])queryString)[0];
83 isbn = StringUtils.remove(
84 StringUtils.removeStart(isbn, "ISBN"), '-').trim();
85 } else {
86 action = Constants.CREATE;
87 }
88 }
89
90 BookForm frm = (BookForm) form;
91
92
93 try {
94 frm.setSubjectList(libraryManager.getSubjects());
95 } catch (Throwable t) {
96 String errString = messages.getMessage("error.subjects.retrieve");
97 logger.error(errString,t);
98 errors.add(new LibraryException(errString,t));
99 return standardForward(mapping,request,errors);
100 }
101
102 if (action.equals(Constants.CREATE)) {
103
104
105 try {
106 Reader rd = libraryManager.retrieveByUid(request.getRemoteUser());
107
108 frm.setOwner(rd.getId());
109 rd = null;
110 } catch (Throwable e) {
111 String errString = messages.getMessage("error.reader.retrieve");
112 logger.error(messages.getMessage(errString,e));
113 errors.add(new LibraryException(errString,e));
114 }
115 }
116
117 if (action.equals(Constants.UPDATE)) {
118 try {
119
120 Book bk = new Book();
121 bk.setId(frm.getId());
122 bk = libraryManager.retrieveBook(bk.getId());
123
124
125 PropertyUtils.copyProperties(frm, bk);
126
127
128 frm.setAuthors(libraryManager.getAuthors( bk));
129 frm.setSubjects(libraryManager.getSubjects( bk ));
130
131
132 Reader rd = libraryManager.retrieveReader(bk.getOwner());
133 frm.setOwnerPhone(rd.getDeskPhone());
134 rd = null;
135
136 } catch (Throwable t) {
137 String errString = messages.getMessage("error.book.retrieve");
138 logger.error(messages.getMessage(errString,t));
139 errors.add(new LibraryException(errString,t));
140 }
141 }
142
143 if (action.equals(Constants.PREFILLBOOKDETAILS)) {
144 try {
145
146 Reader rd = libraryManager.retrieveByUid(
147 request.getRemoteUser());
148
149
150 Map bookDetails = libraryManager.lookupBook(isbn);
151 String title = (String) Utils.getIgnoreCase(
152 bookDetails, Constants.BOOKTITLE);
153 String author = (String) Utils.getIgnoreCase(
154 bookDetails, Constants.BOOKAUTHOR);
155 String publisher = (String) Utils.getIgnoreCase(
156 bookDetails, Constants.BOOKPUBLISHER);
157 String date = (String) Utils.getIgnoreCase(
158 bookDetails, Constants.BOOKDATE);
159 ArrayList subjectStrings = (ArrayList) Utils.getIgnoreCase(
160 bookDetails, Constants.BOOKSUBJECTSLIST);
161
162
163 ArrayList subjects = new ArrayList();
164 for (int i = 0; i < subjectStrings.size(); i++) {
165 subjects.add( new Subject((String)subjectStrings.get(i)));
166 }
167
168
169 Book bk = new Book( title, 0, publisher, date, "", rd.getId(), isbn);
170
171
172 PropertyUtils.copyProperties(frm, bk);
173
174
175
176
177
178
179 ArrayList authors = new ArrayList();
180 authors.add (new Author(author, "") );
181 frm.setAuthors(authors);
182 frm.setSubjects(subjects);
183
184
185 frm.setOwner(rd.getId());
186
187 rd = null;
188
189 } catch (Throwable t) {
190 String errString = messages.getMessage("error.book.prefill", t);
191 logger.error(messages.getMessage(errString,t));
192 errors.add(new LibraryException(errString,t));
193 }
194 }
195
196 return standardForward(mapping,request,errors);
197 }
198 }