001: /*
002: * Copyright 2005-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: package org.kuali.module.kra.document;
017:
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.LinkedHashMap;
021: import java.util.List;
022:
023: import org.kuali.core.bo.AdHocRoutePerson;
024: import org.kuali.core.bo.AdHocRouteWorkgroup;
025: import org.kuali.core.document.Copyable;
026: import org.kuali.core.document.TransactionalDocumentBase;
027: import org.kuali.core.rule.event.KualiDocumentEvent;
028: import org.kuali.core.service.PersistenceService;
029: import org.kuali.kfs.context.SpringContext;
030: import org.kuali.module.kra.bo.AdhocOrg;
031: import org.kuali.module.kra.bo.AdhocPerson;
032: import org.kuali.module.kra.bo.AdhocWorkgroup;
033: import org.kuali.module.kra.service.ResearchDocumentPermissionsService;
034: import org.kuali.module.kra.service.ResearchDocumentService;
035:
036: import edu.iu.uis.eden.exception.WorkflowException;
037:
038: /**
039: * Research Administration Document Base
040: */
041: public abstract class ResearchDocumentBase extends
042: TransactionalDocumentBase implements ResearchDocument, Copyable {
043: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
044: .getLogger(ResearchDocumentBase.class);
045:
046: private List<AdhocPerson> adhocPersons;
047: private List<AdhocOrg> adhocOrgs;
048: private List<AdhocWorkgroup> adhocWorkgroups;
049:
050: /**
051: * Sets up the collection instances and common document attributes.
052: */
053: public ResearchDocumentBase() {
054: super ();
055: adhocPersons = new ArrayList<AdhocPerson>();
056: adhocOrgs = new ArrayList<AdhocOrg>();
057: adhocWorkgroups = new ArrayList<AdhocWorkgroup>();
058: }
059:
060: /**
061: * Overridden to note clear and add notes.
062: *
063: * @see org.kuali.core.document.DocumentBase#toCopy()
064: */
065: @Override
066: public void toCopy() throws WorkflowException,
067: IllegalStateException {
068: super .setNewDocumentHeader();
069: }
070:
071: /**
072: * Gets the adhocPersons attribute.
073: *
074: * @return Returns the adhocPersons.
075: */
076: public List<AdhocPerson> getAdhocPersons() {
077: return adhocPersons;
078: }
079:
080: /**
081: * Sets the adhocPersons attribute value.
082: *
083: * @param adhocPersons The adhocPersons to set.
084: */
085: public void setAdhocPersons(List<AdhocPerson> adhocPersons) {
086: this .adhocPersons = adhocPersons;
087: }
088:
089: /**
090: * Gets the AdhocPerson item at given index.
091: *
092: * @param index
093: * @return AdhocPerson
094: */
095: public AdhocPerson getAdhocPersonItem(int index) {
096: while (this .getAdhocPersons().size() <= index) {
097: this .getAdhocPersons().add(new AdhocPerson());
098: }
099: return this .getAdhocPersons().get(index);
100: }
101:
102: /**
103: * Gets the adhocOrgs attribute.
104: *
105: * @return Returns the adhocOrgs.
106: */
107: public List<AdhocOrg> getAdhocOrgs() {
108: return adhocOrgs;
109: }
110:
111: /**
112: * Sets the adhocOrgs attribute value.
113: *
114: * @param adhocOrgs The adhocOrgs to set.
115: */
116: public void setAdhocOrgs(List<AdhocOrg> adhocOrgs) {
117: this .adhocOrgs = adhocOrgs;
118: }
119:
120: /**
121: * Gets the BudgetAdhocOrg item at given index.
122: *
123: * @param index
124: * @return BudgetAdhocOrg
125: */
126: public AdhocOrg getAdhocOrgItem(int index) {
127: while (this .getAdhocOrgs().size() <= index) {
128: this .getAdhocOrgs().add(new AdhocOrg());
129: }
130: return this .getAdhocOrgs().get(index);
131: }
132:
133: public List<AdhocWorkgroup> getAdhocWorkgroups() {
134: return adhocWorkgroups;
135: }
136:
137: public void setAdhocWorkgroups(List<AdhocWorkgroup> adhocWorkgroups) {
138: this .adhocWorkgroups = adhocWorkgroups;
139: }
140:
141: /**
142: * Gets the AdhocWorkgroup item at given index.
143: *
144: * @param index
145: * @return AdhocWorkgroup
146: */
147: public AdhocWorkgroup getAdhocWorkgroupItem(int index) {
148: while (this .getAdhocWorkgroups().size() <= index) {
149: this .getAdhocWorkgroups().add(new AdhocWorkgroup());
150: }
151: return this .getAdhocWorkgroups().get(index);
152: }
153:
154: /**
155: * Clears all adhocs of a given type.
156: *
157: * @param adhocTypeCode
158: */
159: public void clearAdhocType(String adhocTypeCode) {
160: for (Iterator iter = this .adhocPersons.iterator(); iter
161: .hasNext();) {
162: AdhocPerson person = (AdhocPerson) iter.next();
163: if (adhocTypeCode.equals(person.getAdhocTypeCode())) {
164: iter.remove();
165: }
166: }
167: for (Iterator iter = this .adhocOrgs.iterator(); iter.hasNext();) {
168: AdhocOrg org = (AdhocOrg) iter.next();
169: if (adhocTypeCode.equals(org.getAdhocTypeCode())) {
170: iter.remove();
171: }
172: }
173: for (Iterator iter = this .adhocWorkgroups.iterator(); iter
174: .hasNext();) {
175: AdhocWorkgroup workgroup = (AdhocWorkgroup) iter.next();
176: if (adhocTypeCode.equals(workgroup.getAdhocTypeCode())) {
177: iter.remove();
178: }
179: }
180: }
181:
182: /**
183: * Convert and return this document's adhoc persons as KFS-style AdHoc persons
184: *
185: * @return List<AdHocRoutePerson>
186: */
187: public List<AdHocRoutePerson> convertKraAdhocsToAdHocRoutePersons() {
188: List<AdHocRoutePerson> adHocRoutePersons = new ArrayList<AdHocRoutePerson>();
189: for (AdhocPerson kraAdhocPerson : this .adhocPersons) {
190: SpringContext.getBean(PersistenceService.class)
191: .refreshAllNonUpdatingReferences(kraAdhocPerson);
192: AdHocRoutePerson adHocRoutePerson = new AdHocRoutePerson();
193: adHocRoutePerson.setId(kraAdhocPerson.getUser()
194: .getPersonUserIdentifier());
195: adHocRoutePerson.setActionRequested(kraAdhocPerson
196: .getActionRequested());
197: adHocRoutePersons.add(adHocRoutePerson);
198: }
199: return adHocRoutePersons;
200: }
201:
202: /**
203: * Convert and return this document's adhoc workgroups as KFS-style AdHoc workgroups
204: *
205: * @return List<AdHocRoutePerson>
206: */
207: public List<AdHocRouteWorkgroup> convertKraAdhocsToAdHocRouteWorkgroups() {
208: List<AdHocRouteWorkgroup> adHocRouteWorkgroups = new ArrayList<AdHocRouteWorkgroup>();
209: for (AdhocWorkgroup kraAdhocWorkgroup : this .adhocWorkgroups) {
210: SpringContext.getBean(PersistenceService.class)
211: .refreshAllNonUpdatingReferences(kraAdhocWorkgroup);
212: AdHocRouteWorkgroup adHocRouteWorkgroup = new AdHocRouteWorkgroup();
213: adHocRouteWorkgroup.setId(kraAdhocWorkgroup
214: .getWorkgroupName());
215: adHocRouteWorkgroup.setActionRequested(kraAdhocWorkgroup
216: .getActionRequested());
217: adHocRouteWorkgroups.add(adHocRouteWorkgroup);
218: }
219: return adHocRouteWorkgroups;
220: }
221:
222: /**
223: * Build the xml to use when generating the workflow org routing report.
224: *
225: * @param List<AdhocOrg> orgs
226: * @param boolean encloseContent - whether the generated xml should be enclosed within a <documentContent> tag
227: * @return String
228: */
229: public String buildAdhocOrgReportXml(String permissionTypeCode,
230: boolean encloseContent) {
231: StringBuffer xml = new StringBuffer();
232: if (encloseContent) {
233: xml.append("<documentContent>");
234: }
235: List<AdhocOrg> orgs = SpringContext.getBean(
236: ResearchDocumentPermissionsService.class).getAdHocOrgs(
237: this .getDocumentNumber(), permissionTypeCode);
238: for (AdhocOrg org : orgs) {
239: xml.append("<chartOrg><chartOfAccountsCode>");
240: xml.append(org.getFiscalCampusCode());
241: xml.append("</chartOfAccountsCode><organizationCode>");
242: xml.append(org.getPrimaryDepartmentCode());
243: xml.append("</organizationCode></chartOrg>");
244: }
245: if (encloseContent) {
246: xml.append("</documentContent>");
247: }
248: return xml.toString();
249: }
250:
251: /**
252: * @see org.kuali.core.bo.BusinessObjectBase#toStringMapper()
253: */
254: protected LinkedHashMap toStringMapper() {
255: LinkedHashMap m = new LinkedHashMap();
256:
257: m.put("versionNumber", getVersionNumber());
258: m.put("comp", Boolean.valueOf(getDocumentHeader()
259: .getWorkflowDocument().isCompletionRequested()));
260: m.put("app", Boolean.valueOf(getDocumentHeader()
261: .getWorkflowDocument().isApprovalRequested()));
262: m.put("ack", Boolean.valueOf(getDocumentHeader()
263: .getWorkflowDocument().isAcknowledgeRequested()));
264: m.put("fyi", Boolean.valueOf(getDocumentHeader()
265: .getWorkflowDocument().isFYIRequested()));
266:
267: return m;
268: }
269:
270: @Override
271: public void prepareForSave(KualiDocumentEvent event) {
272: super .prepareForSave(event);
273: try {
274: SpringContext.getBean(ResearchDocumentService.class)
275: .prepareResearchDocumentForSave(this );
276: } catch (Exception e) {
277: throw new RuntimeException(
278: "Error preparing ResearchDocument for save", e);
279: }
280:
281: }
282: }
|