001/**
002 * Copyright 2011-2013 The Kuali Foundation Licensed under the Educational
003 * Community License, Version 2.0 (the "License"); you may not use this file
004 * except in compliance with the License. You may obtain a copy of the License
005 * at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software
010 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
011 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
012 * License for the specific language governing permissions and limitations under
013 * the License.
014 */
015package org.kuali.mobility.academics.service;
016
017import java.util.ArrayList;
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import javax.jws.WebService;
023import javax.servlet.http.HttpServletRequest;
024import javax.ws.rs.FormParam;
025import javax.ws.rs.GET;
026import javax.ws.rs.POST;
027import javax.ws.rs.Path;
028import javax.ws.rs.QueryParam;
029import javax.ws.rs.core.Context;
030
031import javax.servlet.http.HttpServletRequest;
032import javax.servlet.http.HttpSession;
033
034import org.apache.commons.collections.CollectionUtils;
035import org.apache.cxf.jaxrs.ext.MessageContext;
036import org.apache.log4j.Logger;
037import org.kuali.mobility.academics.dao.AcademicsDao;
038import org.kuali.mobility.academics.entity.CareerImpl;
039import org.kuali.mobility.academics.entity.CatalogNumberImpl;
040import org.kuali.mobility.academics.entity.SearchResult;
041import org.kuali.mobility.academics.entity.Section;
042import org.kuali.mobility.academics.entity.SectionImpl;
043import org.kuali.mobility.academics.entity.SubjectImpl;
044import org.kuali.mobility.academics.entity.TermImpl;
045import org.kuali.mobility.academics.util.AcademicsConstants;
046import org.kuali.mobility.academics.util.CareerTransform;
047import org.kuali.mobility.academics.util.CatalogNumberTransform;
048import org.kuali.mobility.academics.util.SectionTransform;
049import org.kuali.mobility.academics.util.SubjectTransform;
050import org.kuali.mobility.academics.util.TermPredicate;
051import org.kuali.mobility.academics.util.TermTransform;
052import org.springframework.context.ApplicationContext;
053import org.springframework.context.ApplicationContextAware;
054import org.springframework.stereotype.Service;
055
056/**
057 *
058 * @author Joe Swanson <joseswan@umich.edu>
059 */
060@Service
061@WebService(endpointInterface = "org.kuali.mobility.academics.service.AcademicsService")
062public class AcademicsServiceImpl implements AcademicsService, ApplicationContextAware {
063
064        private static final Logger LOG = Logger.getLogger(AcademicsServiceImpl.class);
065        private AcademicsDao dao;
066        private ApplicationContext context;
067        @Context
068        private MessageContext messageContext;
069
070        @Override
071        @GET
072        @Path("/getTerms")
073        public List<TermImpl> getTerms() {
074                List<TermImpl> terms = new ArrayList<TermImpl>();
075                CollectionUtils.collect(getDao().getTerms(null), new TermTransform(), terms);
076                return terms;
077        }
078
079        @Override
080        @GET
081        @Path("/getTerm")
082        public TermImpl getTerm(@QueryParam("termId") final String termId) {
083                TermImpl term = (TermImpl) CollectionUtils.find(getDao().getTerms(null), new TermPredicate(termId));
084                return term;
085        }
086
087        @Override
088        @GET
089        @Path("/getCareers")
090        public List<CareerImpl> getCareers(@QueryParam("termId") final String termId) {
091                List<CareerImpl> careers = new ArrayList<CareerImpl>();
092                Map<String, String> query = new HashMap<String, String>();
093                query.put(AcademicsConstants.TERM_ID, termId);
094                CollectionUtils.collect(getDao().getCareers(query), new CareerTransform(), careers);
095                return careers;
096        }
097
098        @Override
099        @GET
100        @Path("/getSubjects")
101        public List<SubjectImpl> getSubjects(
102                        @QueryParam("termId") final String termId,
103                        @QueryParam("careerId") final String careerId) {
104                List<SubjectImpl> subjects = new ArrayList<SubjectImpl>();
105                Map<String, String> query = new HashMap<String, String>();
106                query.put(AcademicsConstants.TERM_ID, termId);
107                query.put(AcademicsConstants.CAREER_ID, careerId);
108                CollectionUtils.collect(getDao().getSubjects(query), new SubjectTransform(), subjects);
109                return subjects;
110        }
111
112        @Override
113        @GET
114        @Path("/getCatalogNumbers")
115        public List<CatalogNumberImpl> getCatalogNumbers(
116                        @QueryParam("termId") final String termId,
117                        @QueryParam("subjectId") final String subjectId) {
118                List<CatalogNumberImpl> numbers = new ArrayList<CatalogNumberImpl>();
119                Map<String, String> query = new HashMap<String, String>();
120                query.put(AcademicsConstants.TERM_ID, termId);
121                query.put(AcademicsConstants.SUBJECT_ID, subjectId);
122                CollectionUtils.collect(getDao().getCatalogNumbers(query), new CatalogNumberTransform(), numbers);
123                return numbers;
124        }
125
126        @SuppressWarnings("unchecked")
127        @Override
128        @GET
129        @Path("/getSections")
130        public List<SectionImpl> getSections(
131                        @QueryParam("termId") final String termId,
132                        @QueryParam("subjectId") final String subjectId,
133                        @QueryParam("catalogNumber") final String catalogNumber,
134                        @QueryParam("careerId") final String careerId) {
135
136                List<SectionImpl> sections = new ArrayList<SectionImpl>();
137
138                LOG.debug("termId is [" + termId + "]");
139                LOG.debug("subjectId is [" + subjectId + "]");
140                LOG.debug("catalogNumber is [" + catalogNumber + "]");
141                LOG.debug("careerId is [" + careerId + "]");
142
143                Map query = new HashMap<String, String>();
144
145                query.put(AcademicsConstants.TERM_ID, termId);
146                query.put(AcademicsConstants.SUBJECT_ID, subjectId);
147                query.put(AcademicsConstants.CATALOG_NUMBER, catalogNumber);
148                query.put(AcademicsConstants.CAREER_ID, careerId);
149
150                LOG.debug("query termId is [" + query.get("termId") + "]");
151                LOG.debug("query subjectId is [" + query.get("subjectId") + "]");
152                LOG.debug("query catalogNumber is [" + query.get("catalogNumber") + "]");
153                LOG.debug("query careerId is [" + query.get("careerId") + "]");
154
155                CollectionUtils.collect(getDao().getSections(query), new SectionTransform(), sections);
156                LOG.debug("Sections is " + sections.size() + " items long.");
157                return sections;
158        }
159
160        @SuppressWarnings("unchecked")
161        @Override
162        @GET
163        @Path("/getSection")
164        public SectionImpl getSection(
165                        @QueryParam("sectionUID") final String sectionUID) {
166                SectionImpl section = null;
167                HttpServletRequest request = (HttpServletRequest) getMessageContext().getHttpServletRequest();
168
169                if (request != null && request.getSession() != null) {
170                        HttpSession session = request.getSession();
171                        if (session.getAttribute(AcademicsConstants.SECTIONS) != null) {
172                                List<Section> allSections = (List<Section>) session.getAttribute(AcademicsConstants.SECTIONS);
173                                for (Section thisSection : allSections) {
174                                        if (sectionUID.equalsIgnoreCase(thisSection.getSectionUID())) {
175                                                SectionTransform transform = new SectionTransform();
176                                                section = transform.transform(getDao().getSectionDetail(thisSection));
177                                                break;
178                                        }
179                                }
180                        }
181                }
182                return section;
183        }
184
185        public SectionImpl getSectionDetail(final Section mySection) {
186                Section section = null;
187                section = getDao().getSectionDetail(mySection);
188
189                SectionTransform transform = new SectionTransform();
190                return transform.transform(section);
191        }
192
193        @Override
194        @POST
195        @Path("/classSearch")
196        public List<SectionImpl> getSearchResults(
197                        @FormParam("term") final String termCode,
198                        @FormParam("careerId") final String careerId,
199                        @FormParam("subject") final String subjectCode,
200                        @FormParam("instructor") final String instructor,
201                        @FormParam("searchCriteria") final String searchCriteria,
202                        @FormParam("filterCriteria") final String filterCriteria,
203                        @FormParam("showOpen") final String showOpen,
204                        @FormParam("distributionReq") final String distributionReq,
205                        @FormParam("otherReq") final String otherReq,
206                        @FormParam("keyword") final String keyword) {
207
208                List<SectionImpl> sections = new ArrayList<SectionImpl>();
209                Map<String, String[]> query = new HashMap<String, String[]>();
210                query.put( AcademicsConstants.TERM_ID, new String[]{termCode} );
211                if (null != careerId && !("0".equalsIgnoreCase(careerId))) {
212                        query.put(AcademicsConstants.CAREER_ID, new String[]{careerId});
213                }
214                if (null != subjectCode && !("0".equalsIgnoreCase(subjectCode))) {
215                        query.put(AcademicsConstants.SUBJECT_ID, new String[]{subjectCode});
216                }
217                query.put(AcademicsConstants.INSTRUCTOR, new String[]{instructor});
218                query.put(AcademicsConstants.SEARCH_TEXT, new String[]{searchCriteria});
219                query.put(AcademicsConstants.FILTER_CRITERIA, new String[]{filterCriteria});
220                query.put(AcademicsConstants.SHOW_OPEN, new String[]{showOpen});
221                // query.put(AcademicsConstants.SHOW_OPEN, distributionReq);
222                // query.put(AcademicsConstants.SHOW_OPEN, otherReq);
223                // query.put(AcademicsConstants.SHOW_OPEN, keyword);
224
225                CollectionUtils.collect(getDao().getSearchResults(query).getSections(), new SectionTransform(), sections);
226
227                // TODO Auto-generated method stub
228                return sections;
229        }
230
231        public SearchResult getSearchResults(Map<String, String[]> query ) {
232            SearchResult result = getDao().getSearchResults(query);
233                    
234            return result;
235        }
236        
237        public void setDao(AcademicsDao dao) {
238                this.dao = dao;
239        }
240
241        public AcademicsDao getDao() {
242                return dao;
243        }
244
245        /**
246         * @return the context
247         */
248        public ApplicationContext getApplicationContext() {
249                return context;
250        }
251
252        /**
253         * @param context the context to set
254         */
255        public void setApplicationContext(ApplicationContext context) {
256                this.context = context;
257        }
258
259        /**
260         * @return the messageContext
261         */
262        public MessageContext getMessageContext() {
263                return messageContext;
264        }
265
266        /**
267         * @param messageContext the messageContext to set
268         */
269        public void setMessageContext(MessageContext messageContext) {
270                this.messageContext = messageContext;
271        }
272}