KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > servlet > SuggestServlet


1 /*
2  * SuggestServlet.java
3  *
4  * Version: $Revision: 1.1 $
5  *
6  * Date: $Date: 2005/11/08 19:22:06 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40
41 package org.dspace.app.webui.servlet;
42
43 import java.io.IOException JavaDoc;
44 import java.sql.SQLException JavaDoc;
45 import java.util.Date JavaDoc;
46 import javax.mail.MessagingException JavaDoc;
47 import javax.servlet.ServletException JavaDoc;
48 import javax.servlet.http.HttpServletRequest JavaDoc;
49 import javax.servlet.http.HttpServletResponse JavaDoc;
50
51
52 import org.apache.log4j.Logger;
53
54 import org.dspace.app.webui.util.JSPManager;
55 import org.dspace.authorize.AuthorizeException;
56 import org.dspace.core.ConfigurationManager;
57 import org.dspace.core.Context;
58 import org.dspace.core.Email;
59 import org.dspace.core.LogManager;
60 import org.dspace.eperson.EPerson;
61 import org.dspace.handle.HandleManager;
62 import org.dspace.content.Item;
63 import org.dspace.content.Collection;
64 import org.dspace.content.DCValue;
65
66
67 /**
68  * Servlet for handling user email recommendations
69  *
70  * @author Arnaldo Dantas
71  * @version $Revision: 1.1 $
72  */

73 public class SuggestServlet extends DSpaceServlet
74 {
75     /** log4j category */
76     private static Logger log = Logger.getLogger(SuggestServlet.class);
77
78     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
79                            HttpServletResponse JavaDoc response)
80         throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc, AuthorizeException
81     {
82         // Obtain information from request
83
String JavaDoc handle = request.getParameter("handle");
84         
85         // Lookup Item title & collection
86
String JavaDoc title = null;
87         String JavaDoc collName = null;
88         if (handle != null && !handle.equals(""))
89         {
90             Item item = (Item) HandleManager.resolveToObject(context, handle);
91             if (item != null)
92             {
93                 DCValue[] titleDC = item.getDC("title", null, Item.ANY);
94                 if (titleDC != null || titleDC.length > 0)
95                 {
96                     title = titleDC[0].value;
97                 }
98                 Collection[] colls = item.getCollections();
99                 collName = colls[0].getMetadata("name");
100             }
101         }
102         else
103         {
104             String JavaDoc path = request.getPathInfo();
105             log.info(LogManager.getHeader(context, "invalid_id", "path=" + path));
106             JSPManager.showInvalidIDError(request, response, path, -1);
107             return;
108         }
109         if (title == null)
110         {
111             title = "";
112         }
113         if(collName == null)
114         {
115             collName = "";
116         }
117         request.setAttribute("suggest.title", title);
118           
119         // User email from context
120
EPerson currentUser = context.getCurrentUser();
121         String JavaDoc authEmail = null;
122         String JavaDoc userName = null;
123         
124         if (currentUser != null)
125         {
126             authEmail = currentUser.getEmail();
127             userName = currentUser.getFullName();
128         }
129         
130         if (request.getParameter("submit") != null)
131         {
132             String JavaDoc recipAddr = request.getParameter("recip_email");
133             // the only required field is recipient email address
134
if (recipAddr == null || recipAddr.equals(""))
135             {
136                 log.info(LogManager.getHeader(context, "show_suggest_form",
137                          "problem=true"));
138                 request.setAttribute("suggest.problem", new Boolean JavaDoc(true));
139                 JSPManager.showJSP(request, response, "/suggest/suggest.jsp");
140                 return;
141             }
142             String JavaDoc recipName = request.getParameter("recip_name");
143             if (recipName == null || "".equals(recipName))
144             {
145                 recipName = ConfigurationManager.getProperty("webui.suggest.recipient");
146             }
147             String JavaDoc senderName = request.getParameter("sender_name");
148             if (senderName == null || "".equals(senderName) )
149             {
150                 // use userName if available
151
if (userName != null)
152                 {
153                     senderName = userName;
154                 }
155                 else
156                 {
157                     // use configured default
158
senderName = ConfigurationManager.getProperty("webui.suggest.sender");
159                 }
160             }
161             String JavaDoc senderAddr = request.getParameter("sender_email");
162             if (senderAddr == null || "".equals(senderAddr) )
163             {
164                 // use authEmail if available
165
if (authEmail != null)
166                 {
167                     senderAddr = authEmail;
168                 }
169             }
170             String JavaDoc itemUri = HandleManager.getCanonicalForm(handle);
171             String JavaDoc itemUrl = HandleManager.resolveToURL(context,handle);
172             String JavaDoc message = request.getParameter("message");
173             String JavaDoc siteName = ConfigurationManager.getProperty("dspace.name");
174
175             // All data is there, send the email
176
try
177             {
178                 Email email = ConfigurationManager.getEmail("suggest");
179                 
180                 email.addRecipient(recipAddr); // recipient address
181

182                 email.addArgument(recipName); // 1st arg - recipient name
183
email.addArgument(senderName); // 2nd arg - sender name
184
email.addArgument(siteName); // 3rd arg - repository name
185
email.addArgument(title); // 4th arg - item title
186
email.addArgument(itemUri); // 5th arg - item handle URI
187
email.addArgument(itemUrl); // 6th arg - item local URL
188
email.addArgument(collName); // 7th arg - collection name
189
email.addArgument(message); // 8th arg - user comments
190

191                 // Set sender's address as 'reply-to' address if supplied
192
if ( senderAddr != null && ! "".equals(senderAddr))
193                 {
194                     email.setReplyTo(senderAddr);
195                 }
196
197                 email.send();
198
199                 log.info(LogManager.getHeader(context, "sent_suggest",
200                                               "from=" + senderAddr));
201
202                 JSPManager.showJSP(request, response, "/suggest/suggest_ok.jsp");
203             }
204             catch (MessagingException JavaDoc me)
205             {
206                 log.warn(LogManager.getHeader(context, "error_mailing_suggest", ""), me);
207                 JSPManager.showInternalError(request, response);
208             }
209         }
210         else
211         {
212             // Display suggest form
213
log.info(LogManager.getHeader(context, "show_suggest_form", "problem=false"));
214             request.setAttribute("authenticated.email", authEmail);
215             request.setAttribute("eperson.name", userName);
216             JSPManager.showJSP(request, response, "/suggest/suggest.jsp"); //asd
217
}
218    }
219
220     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
221                             HttpServletResponse JavaDoc response)
222         throws ServletException JavaDoc, IOException JavaDoc, SQLException JavaDoc, AuthorizeException
223     {
224         // Treat as a GET
225
doDSGet(context, request, response);
226     }
227 }
228
Popular Tags