| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kim.dao.impl; |
| 17 | |
|
| 18 | |
import java.sql.Timestamp; |
| 19 | |
|
| 20 | |
import java.util.List; |
| 21 | |
import java.util.Map; |
| 22 | |
import java.util.Map.Entry; |
| 23 | |
|
| 24 | |
import org.apache.commons.lang.StringUtils; |
| 25 | |
import org.apache.log4j.Logger; |
| 26 | |
import org.apache.ojb.broker.query.Criteria; |
| 27 | |
import org.apache.ojb.broker.query.Query; |
| 28 | |
import org.apache.ojb.broker.query.QueryFactory; |
| 29 | |
import org.apache.ojb.broker.query.ReportQueryByCriteria; |
| 30 | |
import org.kuali.rice.kim.bo.entity.dto.KimPrincipalInfo; |
| 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.bo.types.dto.AttributeDefinitionMap; |
| 35 | |
import org.kuali.rice.kim.bo.types.dto.KimTypeInfo; |
| 36 | |
import org.kuali.rice.kim.dao.KimGroupDao; |
| 37 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
| 38 | |
import org.kuali.rice.kim.service.KIMServiceLocatorInternal; |
| 39 | |
import org.kuali.rice.kim.service.KIMServiceLocatorWeb; |
| 40 | |
import org.kuali.rice.kim.service.KimTypeInfoService; |
| 41 | |
import org.kuali.rice.kim.service.support.KimTypeService; |
| 42 | |
import org.kuali.rice.kim.util.KIMPropertyConstants; |
| 43 | |
import org.kuali.rice.kim.util.KimCommonUtils; |
| 44 | |
import org.kuali.rice.kim.util.KimConstants; |
| 45 | |
import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb; |
| 46 | |
import org.kuali.rice.kns.datadictionary.AttributeDefinition; |
| 47 | |
import org.kuali.rice.kns.datadictionary.BusinessObjectEntry; |
| 48 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
| 49 | |
import org.kuali.rice.kns.service.KNSServiceLocatorWeb; |
| 50 | |
import org.kuali.rice.kns.util.KNSConstants; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 0 | public class KimGroupDaoOjb extends PlatformAwareDaoBaseOjb implements KimGroupDao { |
| 59 | |
|
| 60 | 0 | private static final Logger LOG = Logger.getLogger(KimGroupDaoOjb.class); |
| 61 | |
private KimTypeInfoService kimTypeInfoService; |
| 62 | |
|
| 63 | |
public List<GroupImpl> getGroups(Map<String,String> fieldValues) { |
| 64 | 0 | Criteria crit = new Criteria(); |
| 65 | 0 | BusinessObjectEntry boEntry = KNSServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry("org.kuali.rice.kim.bo.impl.GroupImpl"); |
| 66 | 0 | List lookupNames = boEntry.getLookupDefinition().getLookupFieldNames(); |
| 67 | 0 | String kimTypeId = null; |
| 68 | 0 | for (Map.Entry<String,String> entry : fieldValues.entrySet()) { |
| 69 | 0 | if (entry.getKey().equals("kimTypeId")) { |
| 70 | 0 | kimTypeId=entry.getValue(); |
| 71 | 0 | break; |
| 72 | |
} |
| 73 | |
} |
| 74 | 0 | AttributeDefinitionMap definitions = null; |
| 75 | 0 | for (Entry<String, String> entry : fieldValues.entrySet()) { |
| 76 | 0 | if (StringUtils.isNotBlank(entry.getValue())) { |
| 77 | 0 | if (entry.getKey().contains(".")) { |
| 78 | 0 | Criteria subCrit = new Criteria(); |
| 79 | 0 | String value = entry.getValue().replace('*', '%'); |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | 0 | String[] values = StringUtils.split(value, KNSConstants.OR_LOGICAL_OPERATOR); |
| 84 | 0 | boolean valuesCriterionAdded = false; |
| 85 | 0 | if (values.length > 0) { |
| 86 | 0 | if (definitions == null) { |
| 87 | 0 | KimTypeInfo kimTypeInfo = getKimTypeInfoService().getKimType(kimTypeId); |
| 88 | 0 | KimTypeService kimTypeService = KIMServiceLocatorWeb.getKimTypeService(kimTypeInfo); |
| 89 | 0 | definitions = kimTypeService.getAttributeDefinitions(kimTypeId); |
| 90 | |
} |
| 91 | 0 | AttributeDefinition definition = definitions.getByAttributeName(entry.getKey().substring(0, entry.getKey().indexOf('.'))); |
| 92 | |
|
| 93 | 0 | Criteria valuesCrit = new Criteria(); |
| 94 | 0 | for (int i = 0; i < values.length; i++) { |
| 95 | 0 | String subValue = values[i]; |
| 96 | 0 | if (StringUtils.isNotBlank(subValue)) { |
| 97 | 0 | Criteria valueCrit = new Criteria(); |
| 98 | |
|
| 99 | 0 | if (!Boolean.FALSE.equals(definition.getForceUppercase())) { |
| 100 | 0 | valueCrit.addLike(getDbPlatform().getUpperCaseFunction() + "(attributeValue)", subValue.toUpperCase()); |
| 101 | |
} |
| 102 | |
else { |
| 103 | 0 | valueCrit.addLike("attributeValue", subValue); |
| 104 | |
} |
| 105 | 0 | valuesCriterionAdded = true; |
| 106 | 0 | valuesCrit.addOrCriteria(valueCrit); |
| 107 | |
} |
| 108 | |
} |
| 109 | 0 | subCrit.addAndCriteria(valuesCrit); |
| 110 | |
|
| 111 | 0 | subCrit.addEqualTo("kimAttributeId",entry.getKey().substring(entry.getKey().indexOf(".")+1, entry.getKey().length())); |
| 112 | 0 | subCrit.addEqualTo("kimTypeId", kimTypeId); |
| 113 | |
|
| 114 | 0 | subCrit.addEqualToField(KIMPropertyConstants.Group.GROUP_ID, Criteria.PARENT_QUERY_PREFIX + KIMPropertyConstants.Group.GROUP_ID); |
| 115 | |
|
| 116 | 0 | ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(GroupAttributeDataImpl.class, subCrit); |
| 117 | 0 | if (valuesCriterionAdded) { |
| 118 | 0 | crit.addExists(subQuery); |
| 119 | |
} |
| 120 | |
} |
| 121 | |
|
| 122 | 0 | } else { |
| 123 | 0 | if (lookupNames.contains(entry.getKey())) { |
| 124 | 0 | String value = entry.getValue().replace('*', '%'); |
| 125 | 0 | String[] values = StringUtils.split(value, KNSConstants.OR_LOGICAL_OPERATOR); |
| 126 | 0 | Criteria valuesCrit = new Criteria(); |
| 127 | 0 | for (int i = 0; i < values.length; i++) { |
| 128 | 0 | String subValue = values[i]; |
| 129 | 0 | if (StringUtils.isNotBlank(subValue)) { |
| 130 | 0 | Criteria valueCrit = new Criteria(); |
| 131 | |
|
| 132 | 0 | if (KimConstants.UniqueKeyConstants.GROUP_NAME.equals(entry.getKey())) { |
| 133 | 0 | valueCrit.addLike(getDbPlatform().getUpperCaseFunction() + "(groupName)", subValue.toUpperCase()); |
| 134 | |
} |
| 135 | |
else { |
| 136 | 0 | valueCrit.addLike(entry.getKey(), subValue); |
| 137 | |
} |
| 138 | 0 | valuesCrit.addOrCriteria(valueCrit); |
| 139 | |
} |
| 140 | |
} |
| 141 | 0 | crit.addAndCriteria(valuesCrit); |
| 142 | |
|
| 143 | 0 | } else { |
| 144 | 0 | if (entry.getKey().equals(KIMPropertyConstants.Person.PRINCIPAL_NAME)) { |
| 145 | |
|
| 146 | |
|
| 147 | 0 | Criteria memberSubCrit = new Criteria(); |
| 148 | 0 | memberSubCrit.addEqualToField(KIMPropertyConstants.Group.GROUP_ID, Criteria.PARENT_QUERY_PREFIX + KIMPropertyConstants.Group.GROUP_ID); |
| 149 | |
|
| 150 | 0 | String principalName = entry.getValue(); |
| 151 | |
|
| 152 | 0 | LOG.debug("Searching on Principal Name: " + entry.getValue()); |
| 153 | 0 | KimPrincipalInfo principalInfo = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName); |
| 154 | |
|
| 155 | 0 | if (principalInfo != null) |
| 156 | |
{ |
| 157 | 0 | LOG.debug("Retrieved Principal: " + principalInfo.getPrincipalName()); |
| 158 | 0 | String principalId = principalInfo.getPrincipalId(); |
| 159 | 0 | LOG.debug("Plugging in Principal ID: " + principalId + "as Member ID"); |
| 160 | 0 | memberSubCrit.addLike(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId); |
| 161 | |
|
| 162 | |
|
| 163 | 0 | Timestamp now = KNSServiceLocator.getDateTimeService().getCurrentTimestamp(); |
| 164 | 0 | Criteria afterActiveFromSubCrit = new Criteria(); |
| 165 | 0 | afterActiveFromSubCrit.addLessOrEqualThan(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE, now); |
| 166 | 0 | Criteria nullActiveFromSubCrit = new Criteria(); |
| 167 | 0 | nullActiveFromSubCrit.addIsNull(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE); |
| 168 | |
|
| 169 | 0 | Criteria ActiveMemberSubCrit1 = new Criteria(); |
| 170 | 0 | ActiveMemberSubCrit1.addOrCriteria(afterActiveFromSubCrit); |
| 171 | 0 | ActiveMemberSubCrit1.addOrCriteria(nullActiveFromSubCrit); |
| 172 | |
|
| 173 | 0 | Criteria afterActiveToSubCrit = new Criteria(); |
| 174 | 0 | afterActiveToSubCrit.addGreaterOrEqualThan(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE, now); |
| 175 | 0 | Criteria nullActiveToSubCrit = new Criteria(); |
| 176 | 0 | nullActiveToSubCrit.addIsNull(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE); |
| 177 | |
|
| 178 | 0 | Criteria ActiveMemberSubCrit2 = new Criteria(); |
| 179 | 0 | ActiveMemberSubCrit2.addOrCriteria(afterActiveToSubCrit); |
| 180 | 0 | ActiveMemberSubCrit2.addOrCriteria(nullActiveToSubCrit); |
| 181 | |
|
| 182 | 0 | memberSubCrit.addAndCriteria(ActiveMemberSubCrit1); |
| 183 | 0 | memberSubCrit.addAndCriteria(ActiveMemberSubCrit2); |
| 184 | 0 | } |
| 185 | |
|
| 186 | |
else |
| 187 | |
{ |
| 188 | 0 | LOG.debug("No Principal ID, plugging in blank string as Member ID"); |
| 189 | 0 | memberSubCrit.addLike(KIMPropertyConstants.GroupMember.MEMBER_ID, ""); |
| 190 | |
} |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | 0 | ReportQueryByCriteria memberSubQuery = QueryFactory.newReportQuery(GroupMemberImpl.class, memberSubCrit); |
| 202 | 0 | crit.addExists(memberSubQuery); |
| 203 | 0 | } |
| 204 | |
} |
| 205 | |
} |
| 206 | |
} |
| 207 | |
} |
| 208 | 0 | Query q = QueryFactory.newQuery(GroupImpl.class, crit); |
| 209 | |
|
| 210 | 0 | return (List)getPersistenceBrokerTemplate().getCollectionByQuery(q); |
| 211 | |
} |
| 212 | |
|
| 213 | |
protected KimTypeInfoService getKimTypeInfoService() { |
| 214 | 0 | if (kimTypeInfoService == null) { |
| 215 | 0 | kimTypeInfoService = KIMServiceLocatorWeb.getTypeInfoService(); |
| 216 | |
} |
| 217 | 0 | return kimTypeInfoService; |
| 218 | |
} |
| 219 | |
} |