View Javadoc

1   package org.marketchangers.prayer;
2   
3   import java.util.Date;
4   import java.util.LinkedHashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   /***
9    * @author <a href="mailto:jniu@wc-group.com">Jianshuo Niu</a>
10   * @author <a href="mailto:mtodd@wc-group.com">Matthew Todd</a>
11   */
12  public class PrayerRequestQuery {
13  	private String categoryName;
14  	private Date endDate;
15  	private String organizationName;
16  	private Set requestorUserIds;
17  	private String searchTerms;
18  	private Date startDate;
19  	private String status;
20  	private boolean dateValid = true;
21  
22  	public PrayerRequestQuery(Map map) {
23  		this.categoryName = get(map, "categoryName");
24  		this.endDate = getDate(map, "endDate");
25  		this.organizationName = get(map, "organizationName");
26  		this.searchTerms = get(map, "searchTerms");
27  		this.startDate = getDate(map, "startDate");
28  		this.status = get(map, "status");
29  
30  		this.requestorUserIds = new LinkedHashSet();
31  
32  		String value = get(map, "requestorUserId");
33  		if (value != null) {
34  			requestorUserIds.add(value);
35  		}
36  
37  	}
38  
39  	// --------------------------------------------------------- Public Methods
40  	public String getCategoryName() {
41  		return categoryName;
42  	}
43  
44  	public Date getEndDate() {
45  		return endDate;
46  	}
47  
48  	public Set getRequestorUserIds() {
49  		return requestorUserIds;
50  	}
51  
52  	public String getOrganizationName() {
53  		return organizationName;
54  	}
55  
56  	public String getSearchTerms() {
57  		return searchTerms;
58  	}
59  
60  	public Date getStartDate() {
61  		return startDate;
62  	}
63  
64  	public String getStatus() {
65  		return status;
66  	}
67  
68  	public boolean equals(Object o) {
69  		if (o == null) {
70  			return false;
71  		}
72  
73  		if (!(o instanceof PrayerRequestQuery)) {
74  			return false;
75  		}
76  
77  		PrayerRequestQuery other = (PrayerRequestQuery) o;
78  
79  		return equals(this.categoryName, other.categoryName)
80  			&& equals(this.endDate, other.endDate)
81  			&& equals(this.organizationName, other.organizationName)
82  			&& equals(this.requestorUserIds, other.requestorUserIds)
83  			&& equals(this.searchTerms, other.searchTerms)
84  			&& equals(this.startDate, other.startDate)
85  			&& equals(this.status, other.status);
86  	}
87  
88  	public boolean hasCategoryName() {
89  		return categoryName != null;
90  	}
91  
92  	public boolean hasEndDate() {
93  		return endDate != null;
94  	}
95  
96  	public boolean hasOrganizationName() {
97  		return organizationName != null;
98  	}
99  
100 	public boolean hasRequestorUserIds() {
101 		return !requestorUserIds.isEmpty();
102 	}
103 
104 	public boolean hasSearchTerms() {
105 		return searchTerms != null;
106 	}
107 
108 	public boolean hasStartDate() {
109 		return startDate != null;
110 	}
111 
112 	public boolean hasStatus() {
113 		return status != null;
114 	}
115 
116 	public int hashCode() {
117 		int result = 11;
118 
119 		result = result * 37 + hashCode(categoryName);
120 		result = result * 37 + hashCode(endDate);
121 		result = result * 37 + hashCode(organizationName);
122 		result = result * 37 + hashCode(requestorUserIds);
123 		result = result * 37 + hashCode(searchTerms);
124 		result = result * 37 + hashCode(startDate);
125 		result = result * 37 + hashCode(status);
126 
127 		return result;
128 	}
129 
130 	// this method tells if the startDate and endDate are valid. 
131 	// If startDate and endDate are null, it is treated as valid. This is our design 
132 	public boolean hasValidDate() {
133 		if ((startDate != null)
134 			&& (endDate != null)
135 			&& (startDate.after(endDate))) {
136 			return false;
137 		}
138 		return dateValid;
139 	}
140 
141 	// ------------------------------------------------------- Private Methods
142 	private String get(Map query, String key) {
143 		if (query.containsKey(key)) {
144 			String value = ((String[]) query.get(key))[0];
145 			return value.equals("") ? null : value;
146 		} else {
147 			return null;
148 		}
149 	}
150 
151 	private Date getDate(Map query, String key) {
152 		String value = get(query, key);
153 
154 		if (value == null) {
155 			return null;
156 		}
157 
158 		Date date = DateUtil.validateDateField(value);
159 		if(date==null){
160 			dateValid=false;
161 		}
162 		return date;
163 	}
164 
165 	private boolean equals(Object lhs, Object rhs) {
166 		return lhs == null ? rhs == null : lhs.equals(rhs);
167 	}
168 
169 	private int hashCode(Object o) {
170 		return o == null ? 0 : o.hashCode();
171 	}
172 }