View Javadoc

1   /**
2    * Copyright 2011-2012 The Kuali Foundation Licensed under the Educational Community
3    * License, Version 2.0 (the "License"); you may not use this file except in
4    * compliance with the License. You may obtain a copy of the License at
5    *
6    * http://www.osedu.org/licenses/ECL-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.kuali.mobility.people.service;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import javax.ws.rs.FormParam;
20  import javax.ws.rs.GET;
21  import javax.ws.rs.POST;
22  import javax.ws.rs.Path;
23  import javax.ws.rs.QueryParam;
24  
25  import org.apache.commons.collections.CollectionUtils;
26  import org.kuali.mobility.people.dao.DirectoryDao;
27  import org.kuali.mobility.people.entity.GroupImpl;
28  import org.kuali.mobility.people.entity.PersonImpl;
29  import org.kuali.mobility.people.entity.SearchCriteria;
30  import org.kuali.mobility.people.entity.SearchResultImpl;
31  import org.kuali.mobility.people.service.util.GroupTransform;
32  import org.kuali.mobility.people.service.util.PersonTransform;
33  import org.springframework.context.ApplicationContext;
34  import org.springframework.context.ApplicationContextAware;
35  
36  /**
37   * Implementation of the <code>DirectoryService</code>
38   * @author Kuali Mobility Team (mobility.collab@kuali.org)
39   * @since
40   */
41  public class DirectoryServiceImpl implements DirectoryService, ApplicationContextAware {
42  
43  	/**
44  	 * A reference to the <code>ApplicationContext</code>.
45  	 */
46  	private ApplicationContext applicationContext;
47  
48  	/**
49  	 * A reference to the <code>DirectoryDao</code>.
50  	 */
51  	private DirectoryDao directoryDao;
52  
53  	/**
54  	 * A reference to the <code>PersonTransform</code>.
55  	 */
56  	private PersonTransform personTransform;
57  	
58  	/**
59  	 * A reference to the <code>GroupTransform</code>.
60  	 */
61  	private GroupTransform groupTransform;
62  
63  	/**
64  	 * Performs a search
65  	 */
66  	@POST
67  	@Path("/search")
68  	public SearchResultImpl search(
69  			@FormParam("searchText") final String searchText,
70  			@FormParam("firstName") final String firstName,
71  			@FormParam("lastName") final String lastName,
72  			@FormParam("username") final String username,
73  			@FormParam("exactness") final String exactness,
74  			@FormParam("status") final String status,
75  			@FormParam("location") final String location) {
76  		SearchCriteria searchCriteria = new SearchCriteria();
77  		searchCriteria = new SearchCriteria();
78  		searchCriteria.setExactness(exactness);
79  		searchCriteria.setFirstName(firstName);
80  		searchCriteria.setLastName(lastName);
81  		searchCriteria.setLocation(location);
82  		searchCriteria.setSearchText(searchText);
83  		searchCriteria.setStatus(status);
84  		searchCriteria.setUserName(username);
85  		SearchResultImpl result = findEntries(searchCriteria);
86  		if (result != null) {
87  			result.setSearchCriteria(searchCriteria);
88  		}
89  		return result;
90  	}
91  
92  	/**
93  	 * Finds entries for a search criteria
94  	 */
95  	@Override
96  	public SearchResultImpl findEntries(SearchCriteria searchCriteria) {
97  		return (SearchResultImpl) directoryDao.findEntries(searchCriteria);
98  	}
99  
100 	/**
101 	 * Searches for a person
102 	 */
103 	@POST
104 	@Path("/person/search")
105 	@Override
106 	public List<PersonImpl> personSearch(
107 			@FormParam("searchText") final String searchText,
108 			@FormParam("firstName") final String firstName,
109 			@FormParam("lastName") final String lastName,
110 			@FormParam("username") final String username,
111 			@FormParam("exactness") final String exactness,
112 			@FormParam("status") final String status,
113 			@FormParam("location") final String location) {
114 		List<PersonImpl> people = new ArrayList<PersonImpl>();
115 
116 		SearchCriteria searchCriteria = new SearchCriteria();
117 		searchCriteria = new SearchCriteria();
118 		searchCriteria.setExactness(exactness);
119 		searchCriteria.setFirstName(firstName);
120 		searchCriteria.setLastName(lastName);
121 		searchCriteria.setLocation(location);
122 		searchCriteria.setSearchText(searchText);
123 		searchCriteria.setStatus(status);
124 		searchCriteria.setUserName(username);
125 
126 		CollectionUtils.collect(
127 				(getDirectoryDao().findEntries(searchCriteria)).getPeople(),
128 				 getPersonTransform(),
129 				 people);
130 
131 		return people;
132 	}
133 
134 	/**
135 	 * Looks up a person
136 	 */
137 	@GET
138 	@Path("/person/lookup")
139 	@Override
140 	public PersonImpl personLookup(@QueryParam("username") final String personId) {
141 		return getPersonTransform().transform(directoryDao.lookupPerson(personId));
142 	}
143 
144 	/**
145 	 * Searches for groups
146 	 */
147 	@GET
148 	@Path("/group/search")
149 	@Override
150 	public List<GroupImpl> findSimpleGroup(@QueryParam("searchTerms") final String groupId) {
151 		List<GroupImpl> groups = new ArrayList<GroupImpl>();
152 		CollectionUtils.collect(directoryDao.findSimpleGroup(groupId), getGroupTransform(), groups);
153 		return groups;
154 	}
155 
156 	/**
157 	 * Looks up a group
158 	 */
159 	@GET
160 	@Path("/group/lookup")
161 	@Override
162 	public GroupImpl groupLookup(@QueryParam("dn") final String groupId) {
163 		return getGroupTransform().transform(directoryDao.lookupGroup(groupId));
164 	}
165 
166 	/**
167 	 * Gets the reference to the <code>DirectoryDao</code>.
168 	 * @return Reference to the <code>DirectoryDao</code>.
169 	 */
170 	public DirectoryDao getDirectoryDao() {
171 		return directoryDao;
172 	}
173 
174 	/**
175 	 * Sets the reference to the <code>DirectoryDao</code>.
176 	 * @param directoryDao Reference to the <code>DirectoryDao</code>.
177 	 */
178 	public void setDirectoryDao(DirectoryDao directoryDao) {
179 		this.directoryDao = directoryDao;
180 	}
181 
182 	/**
183 	 * Gets the reference to the <code>PersonTransform</code>.
184 	 * @return Reference to the <code>PersonTransform</code>.
185 	 */
186 	public PersonTransform getPersonTransform() {
187 		return personTransform;
188 	}
189 
190 	/**
191 	 * Sets the reference to the <code>PersonTransform</code>.
192 	 * @param personTransform Reference to the <code>PersonTransform</code>.
193 	 */
194 	public void setPersonTransform(PersonTransform personTransform) {
195 		this.personTransform = personTransform;
196 	}
197 
198 	/**
199 	 * Gets the reference to the <code>GroupTransform</code>.
200 	 * @return Reference to the <code>GroupTransform</code>.
201 	 */
202 	public GroupTransform getGroupTransform() {
203 		return groupTransform;
204 	}
205 
206 	/**
207 	 * Sets the reference to the <code>GroupTransform</code>.
208 	 * @param groupTransform Reference to the <code>GroupTransform</code>.
209 	 */
210 	public void setGroupTransform(GroupTransform groupTransform) {
211 		this.groupTransform = groupTransform;
212 	}
213 
214 	/**
215 	 * Gets the reference to the <code>ApplicationContext</code>.
216 	 * @return Reference to the <code>ApplicationContext</code>.
217 	 */
218 	public ApplicationContext getApplicationContext() {
219 		return applicationContext;
220 	}
221 
222 	/**
223 	 * Sets the reference to the <code>ApplicationContext</code>.
224 	 * @param applicationContext Reference to the <code>ApplicationContext</code>.
225 	 */
226 	public void setApplicationContext(ApplicationContext applicationContext) {
227 		this.applicationContext = applicationContext;
228 	}
229 }