001/*
002 * Copyright 2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may      obtain a copy of the License at
007 *
008 *      http://www.osedu.org/licenses/ECL-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.student.ui.admin.atp;
017
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022import javax.xml.namespace.QName;
023import org.apache.log4j.Logger;
024import org.kuali.rice.core.api.criteria.Predicate;
025import org.kuali.rice.core.api.criteria.PredicateFactory;
026import org.kuali.rice.core.api.criteria.QueryByCriteria;
027import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
028import org.kuali.rice.krad.lookup.LookupableImpl;
029import org.kuali.rice.krad.web.form.LookupForm;
030import org.kuali.student.common.util.ContextBuilder;
031import org.kuali.student.r2.common.dto.ContextInfo;
032import org.kuali.student.r2.core.atp.dto.AtpAtpRelationInfo;
033import org.kuali.student.r2.core.atp.service.AtpService;
034import org.kuali.student.r2.core.constants.AtpServiceConstants;
035
036
037public class AtpAtpRelationInfoAdminLookupableImpl extends LookupableImpl
038{
039        private static final Logger LOG = Logger.getLogger(AtpAtpRelationInfoAdminLookupableImpl.class);
040        private transient AtpService atpService;
041    private static final long serialVersionUID = 1L;
042        @Override
043        protected List<AtpAtpRelationInfo> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded)
044        {
045                QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
046                List<Predicate> pList = new ArrayList<Predicate>();
047        //Code Changed for JIRA-8997 - SONAR Critical issues - Performance - Inefficient use of keySet iterator instead of entrySet iterator
048                for(Map.Entry<String, String> entry: fieldValues.entrySet()) {
049            String fieldName = entry.getKey();
050            String value = entry.getValue();
051            if (value != null && !value.isEmpty())
052            {
053                if (fieldName.equals("maxResultsToReturn"))
054                {
055                    qBuilder.setMaxResults (Integer.parseInt(value));
056                    continue;
057                }
058                pList.add(PredicateFactory.equal(fieldName, value));
059            }
060        }
061                if (!pList.isEmpty())
062                {
063                        qBuilder.setPredicates(PredicateFactory.and(pList.toArray(new Predicate[pList.size()])));
064                }
065                try
066                {
067                        List<AtpAtpRelationInfo> list = this.getAtpService().searchForAtpAtpRelations(qBuilder.build(), getContextInfo());
068                        return list;
069                }
070                catch (Exception ex) {
071                    throw new RuntimeException(ex);
072                }
073        }
074
075        public void setAtpService(AtpService atpService)
076        {
077                    this.atpService = atpService;
078        }
079
080        public AtpService getAtpService()
081        {
082                if (atpService == null)
083                {
084                        QName qname = new QName(AtpServiceConstants.NAMESPACE,AtpServiceConstants.SERVICE_NAME_LOCAL_PART);
085                        atpService = (AtpService) GlobalResourceLoader.getService(qname);
086                }
087                return this.atpService;
088        }
089
090        private ContextInfo getContextInfo() {
091            return ContextBuilder.loadContextInfo();
092        }
093}
094