001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.user.dao;
018:
019: import java.util.List;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.apache.ojb.broker.query.Criteria;
023: import org.apache.ojb.broker.query.QueryByCriteria;
024: import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
025:
026: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
027: import edu.iu.uis.eden.user.AuthenticationUserId;
028: import edu.iu.uis.eden.user.BaseWorkflowUser;
029: import edu.iu.uis.eden.user.EmplId;
030: import edu.iu.uis.eden.user.UserId;
031: import edu.iu.uis.eden.user.UuId;
032: import edu.iu.uis.eden.user.WorkflowUserId;
033:
034: public class BaseUserDAOOjbImpl extends PersistenceBrokerDaoSupport
035: implements BaseUserDAO {
036:
037: protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
038: .getLogger(getClass());
039:
040: public BaseWorkflowUser getWorkflowUser(UserId userId)
041: throws EdenUserNotFoundException {
042: if (userId == null) {
043: throw new IllegalArgumentException(
044: "UserId must be non-null.");
045: }
046: BaseWorkflowUser user = (BaseWorkflowUser) getPersistenceBrokerTemplate()
047: .getObjectByQuery(
048: new QueryByCriteria(BaseWorkflowUser.class,
049: getUserCriteria(userId)));
050: return getReturnSafeWorkflowUser(user);
051: }
052:
053: /**
054: * Not sure if this is necessary for the simple DAO but we'll leave it in anyway ;)
055: */
056: private BaseWorkflowUser getReturnSafeWorkflowUser(
057: BaseWorkflowUser workflowUser) {
058: // special case handling for the case where we only have a workflow id
059: // and an emplid
060: if (workflowUser != null) {
061: if (workflowUser.getAuthenticationUserId() == null
062: || workflowUser.getAuthenticationUserId()
063: .getAuthenticationId() == null) {
064: workflowUser
065: .setAuthenticationUserId(new AuthenticationUserId(
066: workflowUser.getWorkflowUserId()
067: .getWorkflowId()));
068: }
069: if (workflowUser.getDisplayName() == null) {
070: workflowUser.setDisplayName(workflowUser
071: .getWorkflowUserId().getWorkflowId());
072: }
073: if (workflowUser.getEmailAddress() == null) {
074: workflowUser.setEmailAddress("");
075: }
076: if (workflowUser.getGivenName() == null) {
077: workflowUser.setGivenName("");
078: }
079: if (workflowUser.getLastName() == null) {
080: workflowUser.setLastName(workflowUser
081: .getWorkflowUserId().getWorkflowId());
082: }
083: }
084: return workflowUser;
085: }
086:
087: public List getSearchResults(String lastName, String firstName,
088: String authenticationUserId, String workflowId,
089: String emplId, String uuId) {
090: Criteria crit = new Criteria();
091: if (!StringUtils.isEmpty(lastName)) {
092: crit.addEqualTo("lastName", lastName);
093: }
094: if (!StringUtils.isEmpty(firstName)) {
095: crit.addEqualTo("givenName", firstName);
096: }
097: if (!StringUtils.isEmpty(workflowId)) {
098: crit.addEqualTo("workflowUserId", workflowId);
099: }
100: if (!StringUtils.isEmpty(authenticationUserId)) {
101: crit.addEqualTo("authenticationUserId",
102: authenticationUserId);
103: }
104: if (!StringUtils.isEmpty(emplId)) {
105: crit.addEqualTo("emplId", emplId);
106: }
107: if (!StringUtils.isEmpty(uuId)) {
108: crit.addEqualTo("uuId", uuId);
109: }
110: return (List) getPersistenceBrokerTemplate()
111: .getCollectionByQuery(
112: new QueryByCriteria(BaseWorkflowUser.class,
113: crit));
114: }
115:
116: private Criteria getUserCriteria(UserId userId)
117: throws EdenUserNotFoundException {
118: Criteria crit = new Criteria();
119: if (userId.isEmpty()) {
120: LOG.error("Attempting to lookup user with empty Id "
121: + userId);
122: throw new EdenUserNotFoundException(
123: "Attempting to lookup user with empty Id");
124: }
125: if (userId instanceof EmplId) {
126: LOG.debug("Creating example user with EMPLID "
127: + userId.toString());
128: crit.addEqualTo("emplId", ((EmplId) userId).getEmplId());
129: } else if (userId instanceof UuId) {
130: LOG.debug("Creating example user with UUID "
131: + userId.toString());
132: crit.addEqualTo("uuId", ((UuId) userId).getUuId());
133: } else if (userId instanceof AuthenticationUserId) {
134: LOG
135: .debug("Creating example user with AuthenticationUserId "
136: + ((AuthenticationUserId) userId)
137: .getAuthenticationId());
138: crit.addEqualTo("authenticationUserId",
139: ((AuthenticationUserId) userId)
140: .getAuthenticationId());
141: } else if (userId instanceof WorkflowUserId) {
142: LOG.debug("Creating example user with WorkflowUserId "
143: + ((WorkflowUserId) userId).getWorkflowId());
144: crit.addEqualTo("workflowUserId", ((WorkflowUserId) userId)
145: .getWorkflowId());
146: }
147: return crit;
148: }
149:
150: public void save(BaseWorkflowUser user) {
151: getPersistenceBrokerTemplate().store(user);
152: }
153:
154: }
|