View Javadoc

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  public class GroupDaoJpa implements GroupDao {
51  
52      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          Criteria crit = new Criteria(GroupBo.class.getName());
60          BusinessObjectEntry boEntry = KNSServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry("org.kuali.rice.kim.impl.group.GroupBo");
61          List lookupNames = boEntry.getLookupDefinition().getLookupFieldNames();
62          String kimTypeId = null;
63          for (Map.Entry<String,String> entry : fieldValues.entrySet()) {
64          	if (entry.getKey().equals("kimTypeId")) {
65          		kimTypeId=entry.getValue();
66          		break;
67          	}
68          }
69          AttributeDefinitionMap definitions = null;
70          for (Map.Entry<String, String> entry : fieldValues.entrySet()) {
71          	if (StringUtils.isNotBlank(entry.getValue())) {
72          		if (entry.getKey().contains(".")) {
73          	        Criteria subCrit = new Criteria(GroupAttributeBo.class.getName());
74          			String value = entry.getValue().replace('*', '%');
75  
76                      // obey the DD forceUppercase attribute and allow the OR operator
77                      // subCrit.addLike("attributeValue",value);
78                      String[] values = StringUtils.split(value, KNSConstants.OR_LOGICAL_OPERATOR);
79                      boolean valuesCriterionAdded = false;
80                      if (values.length > 0) {
81                          if (definitions == null) {
82                              KimType kimTypeInfo = getKimTypeInfoService().getKimType(kimTypeId);
83                              KimTypeService kimTypeService = KIMServiceLocatorWeb.getKimTypeService(kimTypeInfo);
84                              definitions = kimTypeService.getAttributeDefinitions(kimTypeId);
85                          }
86                          AttributeDefinition definition = definitions.getByAttributeName(entry.getKey().substring(0, entry.getKey().indexOf('.')));
87  
88                          Criteria valuesCrit = new Criteria(GroupAttributeBo.class.getName());
89                          for (int i = 0; i < values.length; i++) {
90                              String subValue = values[i];
91                              if (StringUtils.isNotBlank(subValue)) {
92                                  Criteria valueCrit = new Criteria(GroupAttributeBo.class.getName());
93                                  // null means uppercase it, so do !Boolean.FALSE.equals
94                                  if (!Boolean.FALSE.equals(definition.getForceUppercase())) {
95                                      valueCrit.like("UPPER(__JPA_ALIAS[[0]]__.value)", subValue.toUpperCase());
96                                  }
97                                  else {
98                                      valueCrit.like("value", subValue);
99                                  }
100                                 valuesCriterionAdded = true;
101                                 valuesCrit.or(valueCrit);
102                             }
103                         }
104                         subCrit.and(valuesCrit);
105 
106                         subCrit.eq("id", entry.getKey().substring(entry.getKey().indexOf(".") + 1, entry.getKey().length()));
107                         subCrit.eq("kimTypeId", kimTypeId);
108 
109                         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                         if (valuesCriterionAdded) {
114                             crit.exists(subCrit);
115                         }
116                     }
117 
118         		} else {
119         			if (lookupNames.contains(entry.getKey())) {
120             			String value = entry.getValue().replace('*', '%');
121                         String[] values = StringUtils.split(value, KNSConstants.OR_LOGICAL_OPERATOR);
122                         Criteria valuesCrit = new Criteria(GroupBo.class.getName());
123                         for (int i = 0; i < values.length; i++) {
124                             String subValue = values[i];
125                             if (StringUtils.isNotBlank(subValue)) {
126                                 Criteria valueCrit = new Criteria(GroupBo.class.getName());
127                                 // null means uppercase it, so do !Boolean.FALSE.equals
128                                 if (KimConstants.UniqueKeyConstants.GROUP_NAME.equals(entry.getKey())) {
129                                     valueCrit.like("UPPER(__JPA_ALIAS[[0]]__.name)", subValue.toUpperCase());
130                                 }
131                                 else {
132                                     valueCrit.like(entry.getKey(), subValue);
133                                 }
134                                 valuesCrit.or(valueCrit);
135                             }
136                         }
137                         crit.and(valuesCrit);
138 
139         			} else {
140         				if (entry.getKey().equals(KIMPropertyConstants.Person.PRINCIPAL_NAME)) {
141 
142         					// KULRICE-4248: Retrieve Principal using the Identity Management Service
143         					Criteria memberSubCrit = new Criteria(GroupMemberBo.class.getName());
144         					memberSubCrit.eq(KIMPropertyConstants.GroupMember.GROUP_ID, crit.getAlias() +"."+ KIMPropertyConstants.Group.GROUP_ID);
145         					// Get the passed-in Principal Name
146         					String principalName = entry.getValue();
147         					// Search for the Principal using the Identity Management service
148         					LOG.debug("Searching on Principal Name: " + entry.getValue());
149         					Principal principalInfo = KimApiServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName);
150         					// If a Principal is returned, plug in the Principal ID as the Member ID
151         					if (principalInfo != null)
152         					{
153         						LOG.debug("Retrieved Principal: " + principalInfo.getPrincipalName());
154         						String principalId = principalInfo.getPrincipalId();
155         						LOG.debug("Plugging in Principal ID: " + principalId + "as Member ID");
156         						memberSubCrit.like(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId);
157 
158                    	        	// KULRICE-4232: Only return groups that the principal is an active member of.
159                    	        	Timestamp now = CoreApiServiceLocator.getDateTimeService().getCurrentTimestamp();
160                    	        	Criteria afterActiveFromSubCrit = new Criteria(GroupMemberBo.class.getName());
161                    	        	afterActiveFromSubCrit.lte(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE, now);
162                     	        Criteria nullActiveFromSubCrit = new Criteria(GroupMemberBo.class.getName());
163                     	        nullActiveFromSubCrit.isNull(KIMPropertyConstants.GroupMember.ACTIVE_FROM_DATE);
164 
165 
166                     	        Criteria ActiveMemberSubCrit1 = new Criteria(GroupMemberBo.class.getName());
167                                 ActiveMemberSubCrit1.or(afterActiveFromSubCrit);
168                     	        ActiveMemberSubCrit1.or(nullActiveFromSubCrit);
169 
170 
171                    	        	Criteria afterActiveToSubCrit = new Criteria(GroupMemberBo.class.getName());
172                                 afterActiveToSubCrit.gte(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE, now);
173                     	        Criteria nullActiveToSubCrit = new Criteria(GroupMemberBo.class.getName());
174                     	        nullActiveToSubCrit.isNull(KIMPropertyConstants.GroupMember.ACTIVE_TO_DATE);
175 
176                     	        Criteria ActiveMemberSubCrit2 = new Criteria(GroupMemberBo.class.getName());
177                     	        ActiveMemberSubCrit2.or(afterActiveToSubCrit);
178                     	        ActiveMemberSubCrit2.or(nullActiveToSubCrit);
179 
180                     	        memberSubCrit.and(ActiveMemberSubCrit1);
181                     	        memberSubCrit.and(ActiveMemberSubCrit2);
182         					}
183         					// Otherwise, plug in a blank string as the Member ID
184         					else
185                 	        {
186         						LOG.debug("No Principal ID, plugging in blank string as Member ID");
187         						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                 			crit.exists(memberSubCrit);
201         				}
202         			}
203         		}
204         	}
205         }
206         List<GroupBo> groups = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
207         //Query q = QueryFactory.newQuery(GroupBo.class, crit);
208 
209         return groups;
210     }
211 
212     public EntityManager getEntityManager() {
213         return this.entityManager;
214     }
215 
216     public void setEntityManager(EntityManager entityManager) {
217         this.entityManager = entityManager;
218     }
219 
220     protected KimTypeInfoService getKimTypeInfoService() {
221     	if (kimTypeInfoService == null) {
222     		kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
223     	}
224     	return kimTypeInfoService;
225     }
226 }