Coverage Report - org.kuali.rice.kim.impl.group.GroupDaoOjb
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupDaoOjb
0%
0/95
0%
0/36
10
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 17  
 package org.kuali.rice.kim.impl.group;
 18  
 
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.apache.log4j.Logger;
 21  
 import org.apache.ojb.broker.query.Criteria;
 22  
 import org.apache.ojb.broker.query.Query;
 23  
 import org.apache.ojb.broker.query.QueryFactory;
 24  
 import org.apache.ojb.broker.query.ReportQueryByCriteria;
 25  
 import org.kuali.rice.core.api.CoreApiServiceLocator;
 26  
 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
 27  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 28  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 29  
 import org.kuali.rice.kim.api.type.KimType;
 30  
 import org.kuali.rice.kim.api.type.KimTypeInfoService;
 31  
 import org.kuali.rice.kim.api.type.KimTypeService;
 32  
 import org.kuali.rice.kim.bo.types.dto.AttributeDefinitionMap;
 33  
 import org.kuali.rice.kim.service.KIMServiceLocatorWeb;
 34  
 import org.kuali.rice.kim.util.KIMPropertyConstants;
 35  
 import org.kuali.rice.kim.util.KimConstants;
 36  
 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
 37  
 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
 38  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 39  
 import org.kuali.rice.krad.util.KRADConstants;
 40  
 
 41  
 import java.sql.Timestamp;
 42  
 import java.util.List;
 43  
 import java.util.Map;
 44  
 
 45  
 
 46  0
 public class GroupDaoOjb extends PlatformAwareDaoBaseOjb implements GroupDao  {
 47  
             // KULRICE-4248 Adding logger
 48  0
         private static final Logger LOG = Logger.getLogger(GroupDaoOjb.class);
 49  
         private KimTypeInfoService kimTypeInfoService;
 50  
 
 51  
     public List<GroupBo> getGroups(Map<String,String> fieldValues) {
 52  0
         Criteria crit = new Criteria();
 53  0
         BusinessObjectEntry boEntry = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry("org.kuali.rice.kim.impl.group.GroupBo");
 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
         AttributeDefinitionMap definitions = null;
 63  0
         for (Map.Entry<String, String> entry : fieldValues.entrySet()) {
 64  0
                 if (StringUtils.isNotBlank(entry.getValue())) {
 65  0
                         if (entry.getKey().contains(".")) {
 66  0
                         Criteria subCrit = new Criteria();
 67  0
                                 String value = entry.getValue().replace('*', '%');
 68  
 
 69  
                     // obey the DD forceUppercase attribute and allow the OR operator
 70  
                     // subCrit.addLike("attributeValue",value);
 71  0
                     String[] values = StringUtils.split(value, KRADConstants.OR_LOGICAL_OPERATOR);
 72  0
                     boolean valuesCriterionAdded = false;
 73  0
                     if (values.length > 0) {
 74  0
                         if (definitions == null) {
 75  0
                             KimType kimTypeInfo = getKimTypeInfoService().getKimType(kimTypeId);
 76  0
                             KimTypeService kimTypeService = KIMServiceLocatorWeb.getKimTypeService(kimTypeInfo);
 77  0
                             definitions = kimTypeService.getAttributeDefinitions(kimTypeId);
 78  
                         }
 79  0
                         AttributeDefinition definition = definitions.getByAttributeName(entry.getKey().substring(0, entry.getKey().indexOf('.')));
 80  
 
 81  0
                         Criteria valuesCrit = new Criteria();
 82  0
                         for (int i = 0; i < values.length; i++) {
 83  0
                             String subValue = values[i];
 84  0
                             if (StringUtils.isNotBlank(subValue)) {
 85  0
                                 Criteria valueCrit = new Criteria();
 86  
                                 // null means uppercase it, so do !Boolean.FALSE.equals
 87  0
                                 if (!Boolean.FALSE.equals(definition.getForceUppercase())) {
 88  0
                                     valueCrit.addLike(getDbPlatform().getUpperCaseFunction() + "(value)", subValue.toUpperCase());
 89  
                                 }
 90  
                                 else {
 91  0
                                     valueCrit.addLike("value", subValue);
 92  
                                 }
 93  0
                                 valuesCriterionAdded = true;
 94  0
                                 valuesCrit.addOrCriteria(valueCrit);
 95  
                             }
 96  
                         }
 97  0
                         subCrit.addAndCriteria(valuesCrit);
 98  
 
 99  0
                         subCrit.addEqualTo("id",entry.getKey().substring(entry.getKey().indexOf(".")+1, entry.getKey().length()));
 100  0
                         subCrit.addEqualTo("kimTypeId", kimTypeId);
 101  
 
 102  0
                         subCrit.addEqualToField(KIMPropertyConstants.Group.GROUP_ID, Criteria.PARENT_QUERY_PREFIX + KIMPropertyConstants.Group.GROUP_ID);
 103  
 
 104  0
                         ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(GroupAttributeBo.class, subCrit);
 105  0
                         if (valuesCriterionAdded) {
 106  0
                             crit.addExists(subQuery);
 107  
                         }
 108  
                     }
 109  
 
 110  0
                         } else {
 111  0
                                 if (lookupNames.contains(entry.getKey())) {
 112  0
                                     String value = entry.getValue().replace('*', '%');
 113  0
                         String[] values = StringUtils.split(value, KRADConstants.OR_LOGICAL_OPERATOR);
 114  0
                         Criteria valuesCrit = new Criteria();
 115  0
                         for (int i = 0; i < values.length; i++) {
 116  0
                             String subValue = values[i];
 117  0
                             if (StringUtils.isNotBlank(subValue)) {
 118  0
                                 Criteria valueCrit = new Criteria();
 119  
                                 // null means uppercase it, so do !Boolean.FALSE.equals
 120  0
                                 if (KimConstants.UniqueKeyConstants.GROUP_NAME.equals(entry.getKey())) {
 121  0
                                     valueCrit.addLike(getDbPlatform().getUpperCaseFunction() + "(name)", subValue.toUpperCase());
 122  
                                 }
 123  
                                 else {
 124  0
                                     valueCrit.addLike(entry.getKey(), subValue);
 125  
                                 }
 126  0
                                 valuesCrit.addOrCriteria(valueCrit);
 127  
                             }
 128  
                         }
 129  0
                         crit.addAndCriteria(valuesCrit);
 130  
 
 131  0
                                 } else {
 132  0
                                         if (entry.getKey().equals(KIMPropertyConstants.Person.PRINCIPAL_NAME)) {
 133  
 
 134  
                                                 // KULRICE-4248: Retrieve Principal using the Identity Management Service
 135  0
                                                 Criteria memberSubCrit = new Criteria();
 136  0
                                                 memberSubCrit.addEqualToField(KIMPropertyConstants.Group.GROUP_ID, Criteria.PARENT_QUERY_PREFIX + KIMPropertyConstants.Group.GROUP_ID);
 137  
                                                 // Get the passed-in Principal Name
 138  0
                                                 String principalName = entry.getValue();
 139  
                                                 // Search for the Principal using the Identity Management service
 140  0
                                                 LOG.debug("Searching on Principal Name: " + entry.getValue());
 141  0
                                                 Principal principalInfo = KimApiServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName);
 142  
                                                 // If a Principal is returned, plug in the Principal ID as the Member ID
 143  0
                                                 if (principalInfo != null)
 144  
                                                 {
 145  0
                                                         LOG.debug("Retrieved Principal: " + principalInfo.getPrincipalName());
 146  0
                                                         String principalId = principalInfo.getPrincipalId();
 147  0
                                                         LOG.debug("Plugging in Principal ID: " + principalId + "as Member ID");
 148  0
                                                         memberSubCrit.addLike(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId);
 149  
 
 150  
                                            // KULRICE-4232: Only return groups that the principal is an active member of.
 151  0
                                            Timestamp now = CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp();
 152  0
                                            Criteria afterActiveFromSubCrit = new Criteria();
 153  0
                                            afterActiveFromSubCrit.addLessOrEqualThan(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE, now);
 154  0
                                     Criteria nullActiveFromSubCrit = new Criteria();
 155  0
                                     nullActiveFromSubCrit.addIsNull(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE);
 156  
 
 157  0
                                     Criteria ActiveMemberSubCrit1 = new Criteria();
 158  0
                                     ActiveMemberSubCrit1.addOrCriteria(afterActiveFromSubCrit);
 159  0
                                     ActiveMemberSubCrit1.addOrCriteria(nullActiveFromSubCrit);
 160  
 
 161  0
                                            Criteria afterActiveToSubCrit = new Criteria();
 162  0
                                            afterActiveToSubCrit.addGreaterOrEqualThan(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE, now);
 163  0
                                     Criteria nullActiveToSubCrit = new Criteria();
 164  0
                                     nullActiveToSubCrit.addIsNull(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE);
 165  
 
 166  0
                                     Criteria ActiveMemberSubCrit2 = new Criteria();
 167  0
                                     ActiveMemberSubCrit2.addOrCriteria(afterActiveToSubCrit);
 168  0
                                     ActiveMemberSubCrit2.addOrCriteria(nullActiveToSubCrit);
 169  
 
 170  0
                                     memberSubCrit.addAndCriteria(ActiveMemberSubCrit1);
 171  0
                                     memberSubCrit.addAndCriteria(ActiveMemberSubCrit2);
 172  0
                                                 }
 173  
                                                 // Otherwise, plug in a blank string as the Member ID
 174  
                                                 else
 175  
                                 {
 176  0
                                                         LOG.debug("No Principal ID, plugging in blank string as Member ID");
 177  0
                                                         memberSubCrit.addLike(KIMPropertyConstants.GroupMember.MEMBER_ID, "");
 178  
                                 }
 179  
                                                 /*
 180  
                                 Criteria subCrit = new Criteria();
 181  
                                         String principalName = entry.getValue().replace('*', '%');
 182  
                                         subCrit.addLike(KIMPropertyConstants.Person.PRINCIPAL_NAME, principalName );
 183  
                                 subCrit.addEqualToField(KIMPropertyConstants.Person.PRINCIPAL_ID, Criteria.PARENT_QUERY_PREFIX + "memberId");
 184  
                                         ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(KimPrincipalImpl.class, subCrit);
 185  
                                 Criteria memberSubCrit = new Criteria();
 186  
                                 memberSubCrit.addEqualToField(KIMPropertyConstants.Group.GROUP_ID, Criteria.PARENT_QUERY_PREFIX + KIMPropertyConstants.Group.GROUP_ID);
 187  
                                 memberSubCrit.addExists(subQuery);
 188  
                                 */
 189  0
                                         ReportQueryByCriteria memberSubQuery = QueryFactory.newReportQuery(GroupMemberBo.class, memberSubCrit);
 190  0
                                         crit.addExists(memberSubQuery);
 191  0
                                         }
 192  
                                 }
 193  
                         }
 194  
                 }
 195  
         }
 196  0
         Query q = QueryFactory.newQuery(GroupBo.class, crit);
 197  
 
 198  0
         return (List)getPersistenceBrokerTemplate().getCollectionByQuery(q);
 199  
     }
 200  
 
 201  
     protected KimTypeInfoService getKimTypeInfoService() {
 202  0
             if (kimTypeInfoService == null) {
 203  0
                     kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
 204  
             }
 205  0
             return kimTypeInfoService;
 206  
     }
 207  
 }