001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * Created on Aug 7, 2004
018: *
019: */
020: package org.kuali.module.pdp.dao.ojb;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.ojb.broker.query.Criteria;
028: import org.apache.ojb.broker.query.QueryByCriteria;
029: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
030: import org.kuali.core.exceptions.UserNotFoundException;
031: import org.kuali.core.service.UniversalUserService;
032: import org.kuali.module.pdp.bo.Code;
033: import org.kuali.module.pdp.bo.PdpUser;
034: import org.kuali.module.pdp.bo.UserRequired;
035: import org.kuali.module.pdp.dao.ReferenceDao;
036: import org.kuali.module.pdp.exception.ConfigurationError;
037:
038: /**
039: * @author jsissom
040: */
041: public class ReferenceDaoOjb extends PlatformAwareDaoBaseOjb implements
042: ReferenceDao {
043: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
044: .getLogger(ReferenceDaoOjb.class);
045:
046: private UniversalUserService userService;
047:
048: public ReferenceDaoOjb() {
049: super ();
050: }
051:
052: // Inject
053: public void setUniversalUserService(UniversalUserService us) {
054: userService = us;
055: }
056:
057: private void updateUser(List l) {
058: for (Iterator iter = l.iterator(); iter.hasNext();) {
059: updateUser((Code) iter.next());
060: }
061: }
062:
063: private void updateUser(Code b) {
064: UserRequired ur = (UserRequired) b;
065: try {
066: ur.updateUser(userService);
067: } catch (UserNotFoundException e) {
068: b.setLastUpdateUser(null);
069: }
070: }
071:
072: private Class getClass(String name) {
073: String fullName = "org.kuali.module.pdp.bo." + name;
074:
075: try {
076: return Class.forName(fullName);
077: } catch (ClassNotFoundException e) {
078: throw new ConfigurationError("Unknown type: " + name);
079: }
080: }
081:
082: public Code getCode(String type, String key) {
083: LOG.debug("getCode() for " + type);
084:
085: Criteria criteria = new Criteria();
086: criteria.addEqualTo("code", key);
087:
088: Code code = (Code) getPersistenceBrokerTemplate()
089: .getObjectByQuery(
090: new QueryByCriteria(getClass(type), criteria));
091: if (code != null) {
092: updateUser(code);
093: }
094: return code;
095: }
096:
097: public List getAll(String type) {
098: LOG.debug("getAll() for " + type);
099:
100: QueryByCriteria qbc = new QueryByCriteria(getClass(type));
101: qbc.addOrderBy("description", true);
102:
103: List l = (List) getPersistenceBrokerTemplate()
104: .getCollectionByQuery(qbc);
105: updateUser(l);
106: return l;
107: }
108:
109: public Map getAllMap(String type) {
110: LOG.debug("getAllMap() for " + type);
111:
112: Map hm = new HashMap();
113:
114: for (Iterator iter = getAll(type).iterator(); iter.hasNext();) {
115: Code element = (Code) iter.next();
116: hm.put(element.getCode(), element);
117: }
118: return hm;
119: }
120:
121: public Code addCode(String type, String code, String description,
122: PdpUser u) {
123: Class clazz = getClass(type);
124: Code c;
125:
126: try {
127: c = (Code) clazz.newInstance();
128: } catch (InstantiationException e) {
129: LOG.error("addCode() Can't create instance for " + type, e);
130: throw new ConfigurationError(
131: "Unable to create instance of " + type);
132: } catch (IllegalAccessException e) {
133: LOG.error("addCode() Can't create instance for " + type, e);
134: throw new ConfigurationError(
135: "Unable to create instance of " + type);
136: }
137:
138: c.setCode(code);
139: c.setDescription(description);
140: c.setLastUpdateUser(u);
141:
142: getPersistenceBrokerTemplate().store(c);
143: return c;
144: }
145:
146: // FROM TAFKAT
147: public void updateCode(String code, String description,
148: String type, PdpUser u) {
149: LOG.debug("updateCode() started");
150:
151: Criteria criteria = new Criteria();
152: criteria.addEqualTo("code", code);
153:
154: Code c = (Code) getPersistenceBrokerTemplate()
155: .getObjectByQuery(
156: new QueryByCriteria(getClass(type), criteria));
157:
158: c.setDescription(description);
159: c.setLastUpdateUser(u);
160:
161: getPersistenceBrokerTemplate().store(c);
162:
163: }
164:
165: // OLD PDP
166: public void updateCode(Code item, PdpUser u) {
167: LOG.debug("updateCode() started");
168:
169: item.setLastUpdateUser(u);
170:
171: getPersistenceBrokerTemplate().store(item);
172: }
173:
174: public void deleteCode(Code item) {
175: LOG.debug("deleteCode() started");
176:
177: getPersistenceBrokerTemplate().delete(item);
178: }
179: }
|