1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.mail.search;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.kernel.dao.search.DisplayTerms;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
33  
34  import java.util.Calendar;
35  import java.util.Date;
36  
37  import javax.servlet.http.HttpServletRequest;
38  
39  /**
40   * <a href="MailDisplayTerms.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Alexander Chow
43   *
44   */
45  public class MailDisplayTerms extends DisplayTerms {
46  
47      public static String[] DATE_RANGE_OPTIONS = new String[] {
48          "any-day", "1-day", "3-days", "1-week", "2-weeks", "1-month",
49          "2-months", "6-months", "1-year"
50      };
51  
52      public static String FOLDER_NAME =
53          MailDisplayTerms.class.getName() + "folderName";
54  
55      public static String FROM = MailDisplayTerms.class.getName() + "from";
56  
57      public static String TO = MailDisplayTerms.class.getName() + "to";
58  
59      public static String SUBJECT = MailDisplayTerms.class.getName() + "subject";
60  
61      public static String ENTIRE_MESSAGE =
62          MailDisplayTerms.class.getName() + "entireMessage";
63  
64      public MailDisplayTerms(HttpServletRequest req) {
65          super(req);
66  
67          folderName = ParamUtil.getString(req, FOLDER_NAME);
68          from = ParamUtil.getString(req, FROM);
69          to = ParamUtil.getString(req, TO);
70          subject = ParamUtil.getString(req, SUBJECT);
71          entireMessage = ParamUtil.getString(req, ENTIRE_MESSAGE);
72  
73          try {
74              String range = ParamUtil.getString(req, "dateRange");
75  
76              if (Validator.isNotNull(range) &&
77                  !DATE_RANGE_OPTIONS[0].equals(range)) {
78  
79                  ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
80                      WebKeys.THEME_DISPLAY);
81  
82                  int dateMonth = ParamUtil.getInteger(req, "dateMonth");
83                  int dateDay = ParamUtil.getInteger(req, "dateDay");
84                  int dateYear = ParamUtil.getInteger(req, "dateYear");
85  
86                  Date date =
87                      PortalUtil.getDate(
88                          dateMonth, dateDay, dateYear,
89                          themeDisplay.getTimeZone(), null);
90  
91                  Calendar startCal =
92                      Calendar.getInstance(
93                          themeDisplay.getTimeZone(), themeDisplay.getLocale());
94  
95                  startCal.setTime(date);
96  
97                  Calendar endCal = (Calendar)startCal.clone();
98  
99                  int ordinal = GetterUtil.getInteger(range.substring(0, 1));
100 
101                 if (ordinal > 0) {
102                     if (range.indexOf("day") != -1) {
103                         startCal.add(Calendar.DATE, -ordinal);
104                         endCal.add(Calendar.DATE, ordinal);
105                     }
106                     else if (range.indexOf("week") != -1) {
107                         startCal.add(Calendar.WEEK_OF_YEAR, -ordinal);
108                         endCal.add(Calendar.WEEK_OF_YEAR, ordinal);
109                     }
110                     else if (range.indexOf("month") != -1) {
111                         startCal.add(Calendar.MONTH, -ordinal);
112                         endCal.add(Calendar.MONTH, ordinal);
113                     }
114                     else if (range.indexOf("year") != -1) {
115                         startCal.add(Calendar.YEAR, -ordinal);
116                         endCal.add(Calendar.YEAR, ordinal);
117                     }
118 
119                     startDate = startCal.getTime();
120                     endDate = endCal.getTime();
121                 }
122             }
123         }
124         catch (PortalException pe) {
125         }
126     }
127 
128     public String getFolderName() {
129         return folderName;
130     }
131 
132     public void setFolderName(String folderName) {
133         this.folderName = folderName;
134     }
135 
136     public String getFrom() {
137         return from;
138     }
139 
140     public String getTo() {
141         return to;
142     }
143 
144     public String getSubject() {
145         return subject;
146     }
147 
148     public String getEntireMessage() {
149         return entireMessage;
150     }
151 
152     public boolean hasDate() {
153         if (startDate != null) {
154             return true;
155         }
156         else {
157             return false;
158         }
159     }
160 
161     public Date getStartDate() {
162         return startDate;
163     }
164 
165     public Date getEndDate() {
166         return endDate;
167     }
168 
169     protected String folderName;
170     protected String from;
171     protected String to;
172     protected String subject;
173     protected String entireMessage;
174     protected Date startDate;
175     protected Date endDate;
176 
177 }