Coverage Report - org.kuali.rice.kim.impl.group.GroupDaoJpa
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupDaoJpa
0%
0/96
0%
0/36
5.5
 
 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.impl.group;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.log4j.Logger;
 20  
 import org.kuali.rice.core.api.services.CoreApiServiceLocator;
 21  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
 22  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
 23  
 import org.kuali.rice.kim.api.entity.principal.Principal;
 24  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 25  
 import org.kuali.rice.kim.api.type.KimType;
 26  
 import org.kuali.rice.kim.api.type.KimTypeInfoService;
 27  
 
 28  
 import org.kuali.rice.kim.bo.types.dto.AttributeDefinitionMap;
 29  
 import org.kuali.rice.kim.service.KIMServiceLocatorWeb;
 30  
 import org.kuali.rice.kim.service.support.KimTypeService;
 31  
 import org.kuali.rice.kim.util.KIMPropertyConstants;
 32  
 import org.kuali.rice.kim.util.KimConstants;
 33  
 import org.kuali.rice.kns.datadictionary.AttributeDefinition;
 34  
 import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
 35  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 36  
 import org.kuali.rice.kns.util.KNSConstants;
 37  
 
 38  
 import javax.persistence.EntityManager;
 39  
 import javax.persistence.PersistenceContext;
 40  
 import java.sql.Timestamp;
 41  
 import java.util.List;
 42  
 import java.util.Map;
 43  
 
 44  
 /**
 45  
  * This is a description of what this class does - shyu don't forget to fill this in. 
 46  
  * 
 47  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 48  
  *
 49  
  */
 50  0
 public class GroupDaoJpa implements GroupDao {
 51  
 
 52  0
     private static final Logger LOG = Logger.getLogger(GroupDaoJpa.class);
 53  
     @PersistenceContext(unitName="kim-unit")
 54  
     private EntityManager entityManager;
 55  
     private KimTypeInfoService kimTypeInfoService;
 56  
     
 57  
     public List<GroupBo> getGroups(Map<String,String> fieldValues) {
 58  
 
 59  0
         Criteria crit = new Criteria(GroupBo.class.getName());
 60  0
         BusinessObjectEntry boEntry = KNSServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry("org.kuali.rice.kim.impl.group.GroupBo");
 61  0
         List lookupNames = boEntry.getLookupDefinition().getLookupFieldNames();
 62  0
         String kimTypeId = null;
 63  0
         for (Map.Entry<String,String> entry : fieldValues.entrySet()) {
 64  0
                 if (entry.getKey().equals("kimTypeId")) {
 65  0
                         kimTypeId=entry.getValue();
 66  0
                         break;
 67  
                 }
 68  
         }
 69  0
         AttributeDefinitionMap definitions = null;
 70  0
         for (Map.Entry<String, String> entry : fieldValues.entrySet()) {
 71  0
                 if (StringUtils.isNotBlank(entry.getValue())) {
 72  0
                         if (entry.getKey().contains(".")) {
 73  0
                         Criteria subCrit = new Criteria(GroupAttributeBo.class.getName());
 74  0
                                 String value = entry.getValue().replace('*', '%');
 75  
 
 76  
                     // obey the DD forceUppercase attribute and allow the OR operator
 77  
                     // subCrit.addLike("attributeValue",value);
 78  0
                     String[] values = StringUtils.split(value, KNSConstants.OR_LOGICAL_OPERATOR);
 79  0
                     boolean valuesCriterionAdded = false;
 80  0
                     if (values.length > 0) {
 81  0
                         if (definitions == null) {
 82  0
                             KimType kimTypeInfo = getKimTypeInfoService().getKimType(kimTypeId);
 83  0
                             KimTypeService kimTypeService = KIMServiceLocatorWeb.getKimTypeService(kimTypeInfo);
 84  0
                             definitions = kimTypeService.getAttributeDefinitions(kimTypeId);
 85  
                         }
 86  0
                         AttributeDefinition definition = definitions.getByAttributeName(entry.getKey().substring(0, entry.getKey().indexOf('.')));
 87  
 
 88  0
                         Criteria valuesCrit = new Criteria(GroupAttributeBo.class.getName());
 89  0
                         for (int i = 0; i < values.length; i++) {
 90  0
                             String subValue = values[i];
 91  0
                             if (StringUtils.isNotBlank(subValue)) {
 92  0
                                 Criteria valueCrit = new Criteria(GroupAttributeBo.class.getName());
 93  
                                 // null means uppercase it, so do !Boolean.FALSE.equals
 94  0
                                 if (!Boolean.FALSE.equals(definition.getForceUppercase())) {
 95  0
                                     valueCrit.like("UPPER(__JPA_ALIAS[[0]]__.value)", subValue.toUpperCase());
 96  
                                 }
 97  
                                 else {
 98  0
                                     valueCrit.like("value", subValue);
 99  
                                 }
 100  0
                                 valuesCriterionAdded = true;
 101  0
                                 valuesCrit.or(valueCrit);
 102  
                             }
 103  
                         }
 104  0
                         subCrit.and(valuesCrit);
 105  
 
 106  0
                         subCrit.eq("id", entry.getKey().substring(entry.getKey().indexOf(".") + 1, entry.getKey().length()));
 107  0
                         subCrit.eq("kimTypeId", kimTypeId);
 108  
 
 109  0
                         subCrit.eq(KIMPropertyConstants.Group.GROUP_ID, crit.getAlias() + "." + KIMPropertyConstants.Group.GROUP_ID);
 110  
 
 111  
 
 112  
                         //List<GroupAttributeBo> attributes = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 113  0
                         if (valuesCriterionAdded) {
 114  0
                             crit.exists(subCrit);
 115  
                         }
 116  
                     }
 117  
 
 118  0
                         } else {
 119  0
                                 if (lookupNames.contains(entry.getKey())) {
 120  0
                                     String value = entry.getValue().replace('*', '%');
 121  0
                         String[] values = StringUtils.split(value, KNSConstants.OR_LOGICAL_OPERATOR);
 122  0
                         Criteria valuesCrit = new Criteria(GroupBo.class.getName());
 123  0
                         for (int i = 0; i < values.length; i++) {
 124  0
                             String subValue = values[i];
 125  0
                             if (StringUtils.isNotBlank(subValue)) {
 126  0
                                 Criteria valueCrit = new Criteria(GroupBo.class.getName());
 127  
                                 // null means uppercase it, so do !Boolean.FALSE.equals
 128  0
                                 if (KimConstants.UniqueKeyConstants.GROUP_NAME.equals(entry.getKey())) {
 129  0
                                     valueCrit.like("UPPER(__JPA_ALIAS[[0]]__.name)", subValue.toUpperCase());
 130  
                                 }
 131  
                                 else {
 132  0
                                     valueCrit.like(entry.getKey(), subValue);
 133  
                                 }
 134  0
                                 valuesCrit.or(valueCrit);
 135  
                             }
 136  
                         }
 137  0
                         crit.and(valuesCrit);
 138  
 
 139  0
                                 } else {
 140  0
                                         if (entry.getKey().equals(KIMPropertyConstants.Person.PRINCIPAL_NAME)) {
 141  
 
 142  
                                                 // KULRICE-4248: Retrieve Principal using the Identity Management Service
 143  0
                                                 Criteria memberSubCrit = new Criteria(GroupMemberBo.class.getName());
 144  0
                                                 memberSubCrit.eq(KIMPropertyConstants.GroupMember.GROUP_ID, crit.getAlias() +"."+ KIMPropertyConstants.Group.GROUP_ID);
 145  
                                                 // Get the passed-in Principal Name
 146  0
                                                 String principalName = entry.getValue();
 147  
                                                 // Search for the Principal using the Identity Management service
 148  0
                                                 LOG.debug("Searching on Principal Name: " + entry.getValue());
 149  0
                                                 Principal principalInfo = KimApiServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName);
 150  
                                                 // If a Principal is returned, plug in the Principal ID as the Member ID
 151  0
                                                 if (principalInfo != null)
 152  
                                                 {
 153  0
                                                         LOG.debug("Retrieved Principal: " + principalInfo.getPrincipalName());
 154  0
                                                         String principalId = principalInfo.getPrincipalId();
 155  0
                                                         LOG.debug("Plugging in Principal ID: " + principalId + "as Member ID");
 156  0
                                                         memberSubCrit.like(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId);
 157  
 
 158  
                                            // KULRICE-4232: Only return groups that the principal is an active member of.
 159  0
                                            Timestamp now = CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp();
 160  0
                                            Criteria afterActiveFromSubCrit = new Criteria(GroupMemberBo.class.getName());
 161  0
                                            afterActiveFromSubCrit.lte(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE, now);
 162  0
                                     Criteria nullActiveFromSubCrit = new Criteria(GroupMemberBo.class.getName());
 163  0
                                     nullActiveFromSubCrit.isNull(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE);
 164  
 
 165  
 
 166  0
                                     Criteria ActiveMemberSubCrit1 = new Criteria(GroupMemberBo.class.getName());
 167  0
                                 ActiveMemberSubCrit1.or(afterActiveFromSubCrit);
 168  0
                                     ActiveMemberSubCrit1.or(nullActiveFromSubCrit);
 169  
 
 170  
 
 171  0
                                            Criteria afterActiveToSubCrit = new Criteria(GroupMemberBo.class.getName());
 172  0
                                 afterActiveToSubCrit.gte(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE, now);
 173  0
                                     Criteria nullActiveToSubCrit = new Criteria(GroupMemberBo.class.getName());
 174  0
                                     nullActiveToSubCrit.isNull(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE);
 175  
 
 176  0
                                     Criteria ActiveMemberSubCrit2 = new Criteria(GroupMemberBo.class.getName());
 177  0
                                     ActiveMemberSubCrit2.or(afterActiveToSubCrit);
 178  0
                                     ActiveMemberSubCrit2.or(nullActiveToSubCrit);
 179  
 
 180  0
                                     memberSubCrit.and(ActiveMemberSubCrit1);
 181  0
                                     memberSubCrit.and(ActiveMemberSubCrit2);
 182  0
                                                 }
 183  
                                                 // Otherwise, plug in a blank string as the Member ID
 184  
                                                 else
 185  
                                 {
 186  0
                                                         LOG.debug("No Principal ID, plugging in blank string as Member ID");
 187  0
                                                         memberSubCrit.like(KIMPropertyConstants.GroupMember.MEMBER_ID, "");
 188  
                                 }
 189  
                                                 /*
 190  
                                 Criteria subCrit = new Criteria();
 191  
                                         String principalName = entry.getValue().replace('*', '%');
 192  
                                         subCrit.addLike(KIMPropertyConstants.Person.PRINCIPAL_NAME, principalName );
 193  
                                 subCrit.addEqualToField(KIMPropertyConstants.Person.PRINCIPAL_ID, Criteria.PARENT_QUERY_PREFIX + "memberId");
 194  
                                         ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(KimPrincipalImpl.class, subCrit);
 195  
                                 Criteria memberSubCrit = new Criteria();
 196  
                                 memberSubCrit.addEqualToField(KIMPropertyConstants.Group.GROUP_ID, Criteria.PARENT_QUERY_PREFIX + KIMPropertyConstants.Group.GROUP_ID);
 197  
                                 memberSubCrit.addExists(subQuery);
 198  
                                 */
 199  
                                         //ReportQueryByCriteria memberSubQuery = QueryFactory.newReportQuery(GroupMemberBo.class, memberSubCrit);
 200  0
                                         crit.exists(memberSubCrit);
 201  0
                                         }
 202  
                                 }
 203  
                         }
 204  
                 }
 205  
         }
 206  0
         List<GroupBo> groups = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 207  
         //Query q = QueryFactory.newQuery(GroupBo.class, crit);
 208  
 
 209  0
         return groups;
 210  
     }
 211  
 
 212  
     public EntityManager getEntityManager() {
 213  0
         return this.entityManager;
 214  
     }
 215  
 
 216  
     public void setEntityManager(EntityManager entityManager) {
 217  0
         this.entityManager = entityManager;
 218  0
     }
 219  
 
 220  
     protected KimTypeInfoService getKimTypeInfoService() {
 221  0
             if (kimTypeInfoService == null) {
 222  0
                     kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
 223  
             }
 224  0
             return kimTypeInfoService;
 225  
     }
 226  
 }