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  import javax.ws.rs.*;
19  import org.apache.commons.collections.CollectionUtils;
20  
21  import org.kuali.mobility.people.dao.DirectoryDao;
22  import org.kuali.mobility.people.entity.*;
23  import org.kuali.mobility.people.service.util.GroupTransform;
24  import org.kuali.mobility.people.service.util.PersonTransform;
25  import org.springframework.context.ApplicationContext;
26  import org.springframework.context.ApplicationContextAware;
27  
28  public class DirectoryServiceImpl implements DirectoryService,
29  		ApplicationContextAware {
30  
31  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
32  			.getLogger(DirectoryServiceImpl.class);
33  
34  	private ApplicationContext applicationContext;
35  
36  	private DirectoryDao directoryDao;
37  
38  	private PersonTransform personTransform;
39  	private GroupTransform groupTransform;
40  
41  	@POST
42  	@Path("/search")
43  	public SearchResultImpl search(
44  			@FormParam("searchText") final String searchText,
45  			@FormParam("firstName") final String firstName,
46  			@FormParam("lastName") final String lastName,
47  			@FormParam("username") final String username,
48  			@FormParam("exactness") final String exactness,
49  			@FormParam("status") final String status,
50  			@FormParam("location") final String location) {
51  		SearchCriteria searchCriteria = new SearchCriteria();
52  		searchCriteria = new SearchCriteria();
53  		searchCriteria.setExactness(exactness);
54  		searchCriteria.setFirstName(firstName);
55  		searchCriteria.setLastName(lastName);
56  		searchCriteria.setLocation(location);
57  		searchCriteria.setSearchText(searchText);
58  		searchCriteria.setStatus(status);
59  		searchCriteria.setUserName(username);
60  		SearchResultImpl result = findEntries(searchCriteria);
61  		if (result != null) {
62  			result.setSearchCriteria(searchCriteria);
63  		}
64  		return result;
65  	}
66  
67  	@Override
68  	public SearchResultImpl findEntries(SearchCriteria searchCriteria) {
69  		return (SearchResultImpl) directoryDao.findEntries(searchCriteria);
70  	}
71  
72  	@POST
73  	@Path("/person/search")
74  	@Override
75  	public List<PersonImpl> personSearch(
76  			@FormParam("searchText") final String searchText,
77  			@FormParam("firstName") final String firstName,
78  			@FormParam("lastName") final String lastName,
79  			@FormParam("username") final String username,
80  			@FormParam("exactness") final String exactness,
81  			@FormParam("status") final String status,
82  			@FormParam("location") final String location) {
83  		List<PersonImpl> people = new ArrayList<PersonImpl>();
84  
85  		SearchCriteria searchCriteria = new SearchCriteria();
86  		searchCriteria = new SearchCriteria();
87  		searchCriteria.setExactness(exactness);
88  		searchCriteria.setFirstName(firstName);
89  		searchCriteria.setLastName(lastName);
90  		searchCriteria.setLocation(location);
91  		searchCriteria.setSearchText(searchText);
92  		searchCriteria.setStatus(status);
93  		searchCriteria.setUserName(username);
94  
95  		CollectionUtils.collect(
96  				(getDirectoryDao().findEntries(searchCriteria)).getPeople(),
97  				getPersonTransform(), people);
98  
99  		return people;
100 	}
101 
102 	@GET
103 	@Path("/person/lookup")
104 	@Override
105 	public PersonImpl personLookup(@QueryParam("username") final String personId) {
106 		PersonImpl person = null;
107 		LOG.debug("Searching for " + personId);
108 		person = getPersonTransform().transform(
109 				directoryDao.lookupPerson(personId));
110 		LOG.debug("Person object " + (null == person ? "is" : "is not")
111 				+ " null");
112 		return person;
113 	}
114 
115 	@GET
116 	@Path("/group/search")
117 	@Override
118 	public List<GroupImpl> findSimpleGroup(
119 			@QueryParam("searchTerms") final String groupId) {
120 		List<GroupImpl> groups = new ArrayList<GroupImpl>();
121 
122 		CollectionUtils.collect(directoryDao.findSimpleGroup(groupId),
123 				getGroupTransform(), groups);
124 
125 		return groups;
126 	}
127 
128 	@GET
129 	@Path("/group/lookup")
130 	@Override
131 	public GroupImpl groupLookup(@QueryParam("dn") final String groupId) {
132 		GroupImpl group = null;
133 		group = getGroupTransform()
134 				.transform(directoryDao.lookupGroup(groupId));
135 		return group;
136 	}
137 
138 	public DirectoryDao getDirectoryDao() {
139 		return directoryDao;
140 	}
141 
142 	public void setDirectoryDao(DirectoryDao directoryDao) {
143 		this.directoryDao = directoryDao;
144 	}
145 
146 	/**
147 	 * @return the personTransform
148 	 */
149 	public PersonTransform getPersonTransform() {
150 		return personTransform;
151 	}
152 
153 	/**
154 	 * @param personTransform
155 	 *            the personTransform to set
156 	 */
157 	public void setPersonTransform(PersonTransform personTransform) {
158 		this.personTransform = personTransform;
159 	}
160 
161 	/**
162 	 * @return the groupTransform
163 	 */
164 	public GroupTransform getGroupTransform() {
165 		return groupTransform;
166 	}
167 
168 	/**
169 	 * @param groupTransform
170 	 *            the groupTransform to set
171 	 */
172 	public void setGroupTransform(GroupTransform groupTransform) {
173 		this.groupTransform = groupTransform;
174 	}
175 
176 	/**
177 	 * @return the applicationContext
178 	 */
179 	public ApplicationContext getApplicationContext() {
180 		return applicationContext;
181 	}
182 
183 	/**
184 	 * @param applicationContext
185 	 *            the applicationContext to set
186 	 */
187 	public void setApplicationContext(ApplicationContext applicationContext) {
188 		this.applicationContext = applicationContext;
189 	}
190 }