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