Coverage Report - org.kuali.rice.kim.dao.impl.KimGroupDaoOjb
 
Classes in this File Line Coverage Branch Coverage Complexity
KimGroupDaoOjb
0%
0/63
0%
0/18
10
 
 1  
 /*
 2  
  * Copyright 2007-2009 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kim.dao.impl;
 17  
 
 18  
 import java.sql.Timestamp;
 19  
 import java.util.List;
 20  
 import java.util.Map;
 21  
 import java.util.Map.Entry;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.apache.log4j.Logger;
 25  
 import org.apache.ojb.broker.query.Criteria;
 26  
 import org.apache.ojb.broker.query.Query;
 27  
 import org.apache.ojb.broker.query.QueryFactory;
 28  
 import org.apache.ojb.broker.query.ReportQueryByCriteria;
 29  
 import org.kuali.rice.kim.bo.entity.dto.KimPrincipalInfo;
 30  
 import org.kuali.rice.kim.bo.entity.impl.KimPrincipalImpl;
 31  
 import org.kuali.rice.kim.bo.group.impl.GroupAttributeDataImpl;
 32  
 import org.kuali.rice.kim.bo.group.impl.GroupMemberImpl;
 33  
 import org.kuali.rice.kim.bo.impl.GroupImpl;
 34  
 import org.kuali.rice.kim.dao.KimGroupDao;
 35  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 36  
 import org.kuali.rice.kim.util.KIMPropertyConstants;
 37  
 import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
 38  
 import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
 39  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 40  
 
 41  
 /**
 42  
  * This is a description of the KimGroupDaoOjb class.
 43  
  *
 44  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 45  
  *
 46  
  */
 47  0
 public class KimGroupDaoOjb extends PlatformAwareDaoBaseOjb implements KimGroupDao {
 48  
         // KULRICE-4248 Adding logger
 49  0
         private static final Logger LOG = Logger.getLogger(KimGroupDaoOjb.class);
 50  
 
 51  
     public List<GroupImpl> getGroups(Map<String,String> fieldValues) {
 52  0
         Criteria crit = new Criteria();
 53  0
         BusinessObjectEntry boEntry = KNSServiceLocator.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry("org.kuali.rice.kim.bo.impl.GroupImpl");
 54  0
         List lookupNames = boEntry.getLookupDefinition().getLookupFieldNames();
 55  0
         String kimTypeId = null;
 56  0
         for (Map.Entry<String,String> entry : fieldValues.entrySet()) {
 57  0
                 if (entry.getKey().equals("kimTypeId")) {
 58  0
                         kimTypeId=entry.getValue();
 59  0
                         break;
 60  
                 }
 61  
         }
 62  0
         for (Entry<String, String> entry : fieldValues.entrySet()) {
 63  0
                 if (StringUtils.isNotBlank(entry.getValue())) {
 64  0
                         if (entry.getKey().contains(".")) {
 65  0
                         Criteria subCrit = new Criteria();
 66  0
                                 String value = entry.getValue().replace('*', '%');
 67  
 
 68  0
                                 subCrit.addLike("attributeValue",value);
 69  0
                                 subCrit.addEqualTo("kimAttributeId",entry.getKey().substring(entry.getKey().indexOf(".")+1, entry.getKey().length()));
 70  0
                                 subCrit.addEqualTo("kimTypeId", kimTypeId);
 71  0
                                 ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(GroupAttributeDataImpl.class, subCrit);
 72  0
                                 crit.addExists(subQuery);
 73  0
                         } else {
 74  0
                                 if (lookupNames.contains(entry.getKey())) {
 75  0
                                     String value = entry.getValue().replace('*', '%');
 76  0
                                         if(entry.getKey().equalsIgnoreCase(KIMPropertyConstants.Group.GROUP_NAME)) {
 77  0
                                                 crit.addLike(getDbPlatform().getUpperCaseFunction() + "(" + entry.getKey() + ")", value.toUpperCase());
 78  
                                         }
 79  
                                         else {
 80  0
                             crit.addLike((entry.getKey()), value);
 81  
                                         }
 82  0
                                 } else {
 83  0
                                         if (entry.getKey().equals(KIMPropertyConstants.Person.PRINCIPAL_NAME)) {
 84  
 
 85  
                                                 // KULRICE-4248: Retrieve Principal using the Identity Management Service
 86  0
                                                 Criteria memberSubCrit = new Criteria();
 87  0
                                                 memberSubCrit.addEqualToField(KIMPropertyConstants.Group.GROUP_ID, Criteria.PARENT_QUERY_PREFIX + KIMPropertyConstants.Group.GROUP_ID);
 88  
                                                 // Get the passed-in Principal Name
 89  0
                                                 String principalName = entry.getValue();
 90  
                                                 // Search for the Principal using the Identity Management service
 91  0
                                                 LOG.debug("Searching on Principal Name: " + entry.getValue());
 92  0
                                                 KimPrincipalInfo principalInfo = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName);
 93  
                                                 // If a Principal is returned, plug in the Principal ID as the Member ID
 94  0
                                                 if (principalInfo != null)
 95  
                                                 {
 96  0
                                                         LOG.debug("Retrieved Principal: " + principalInfo.getPrincipalName());
 97  0
                                                         String principalId = principalInfo.getPrincipalId();
 98  0
                                                         LOG.debug("Plugging in Principal ID: " + principalId + "as Member ID");
 99  0
                                                         memberSubCrit.addLike(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId);
 100  
 
 101  
                                            // KULRICE-4232: Only return groups that the principal is an active member of.
 102  0
                                            Timestamp now = KNSServiceLocator.getDateTimeService().getCurrentTimestamp();
 103  0
                                            Criteria afterActiveFromSubCrit = new Criteria();
 104  0
                                            afterActiveFromSubCrit.addLessOrEqualThan(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE, now);
 105  0
                                     Criteria nullActiveFromSubCrit = new Criteria();
 106  0
                                     nullActiveFromSubCrit.addIsNull(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE);
 107  
                                     
 108  0
                                     Criteria ActiveMemberSubCrit1 = new Criteria();
 109  0
                                     ActiveMemberSubCrit1.addOrCriteria(afterActiveFromSubCrit);
 110  0
                                     ActiveMemberSubCrit1.addOrCriteria(nullActiveFromSubCrit);
 111  
                                     
 112  0
                                            Criteria afterActiveToSubCrit = new Criteria();
 113  0
                                            afterActiveToSubCrit.addGreaterOrEqualThan(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE, now);
 114  0
                                     Criteria nullActiveToSubCrit = new Criteria();
 115  0
                                     nullActiveToSubCrit.addIsNull(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE);
 116  
                                     
 117  0
                                     Criteria ActiveMemberSubCrit2 = new Criteria();
 118  0
                                     ActiveMemberSubCrit2.addOrCriteria(afterActiveToSubCrit);
 119  0
                                     ActiveMemberSubCrit2.addOrCriteria(nullActiveToSubCrit);
 120  
                                            
 121  0
                                     memberSubCrit.addAndCriteria(ActiveMemberSubCrit1);
 122  0
                                     memberSubCrit.addAndCriteria(ActiveMemberSubCrit2);                        
 123  0
                                                 }
 124  
                                                 // Otherwise, plug in a blank string as the Member ID
 125  
                                                 else
 126  
                                 {
 127  0
                                                         LOG.debug("No Principal ID, plugging in blank string as Member ID");
 128  0
                                                         memberSubCrit.addLike(KIMPropertyConstants.GroupMember.MEMBER_ID, "");
 129  
                                 }
 130  
                                                 /*
 131  
                                 Criteria subCrit = new Criteria();
 132  
                                         String principalName = entry.getValue().replace('*', '%');
 133  
                                         subCrit.addLike(KIMPropertyConstants.Person.PRINCIPAL_NAME, principalName );
 134  
                                 subCrit.addEqualToField(KIMPropertyConstants.Person.PRINCIPAL_ID, Criteria.PARENT_QUERY_PREFIX + "memberId");
 135  
                                         ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(KimPrincipalImpl.class, subCrit);
 136  
                                 Criteria memberSubCrit = new Criteria();
 137  
                                 memberSubCrit.addEqualToField(KIMPropertyConstants.Group.GROUP_ID, Criteria.PARENT_QUERY_PREFIX + KIMPropertyConstants.Group.GROUP_ID);
 138  
                                 memberSubCrit.addExists(subQuery);
 139  
                                 */
 140  0
                                         ReportQueryByCriteria memberSubQuery = QueryFactory.newReportQuery(GroupMemberImpl.class, memberSubCrit);
 141  0
                                         crit.addExists(memberSubQuery);
 142  0
                                         }
 143  
                                 }
 144  
                         }
 145  
                 }
 146  
         }
 147  0
         Query q = QueryFactory.newQuery(GroupImpl.class, crit);
 148  
 
 149  0
         return (List)getPersistenceBrokerTemplate().getCollectionByQuery(q);
 150  
     }
 151  
 
 152  
 }