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