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: package org.kuali.module.chart.maintenance;
017:
018: import java.security.GeneralSecurityException;
019: import java.util.List;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.kuali.core.document.MaintenanceLock;
023: import org.kuali.core.maintenance.KualiMaintainableImpl;
024: import org.kuali.core.service.DataDictionaryService;
025: import org.kuali.core.service.DateTimeService;
026: import org.kuali.core.service.EncryptionService;
027: import org.kuali.core.util.ObjectUtils;
028: import org.kuali.kfs.KFSConstants;
029: import org.kuali.kfs.context.SpringContext;
030: import org.kuali.module.chart.bo.Delegate;
031: import org.kuali.rice.KNSServiceLocator;
032:
033: /**
034: * This class is a special implementation of Maintainable specifically for Account Delegates. It was created to correctly update the
035: * default Start Date on edits and copies, ala JIRA #KULRNE-62.
036: */
037: public class KualiDelegateMaintainableImpl extends
038: KualiMaintainableImpl {
039: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
040: .getLogger(KualiDelegateMaintainableImpl.class);
041:
042: /**
043: * This method will reset AccountDelegate's Start Date to the current timestamp on edits and copies
044: *
045: * @see org.kuali.core.maintenance.KualiMaintainableImpl#processAfterRetrieve()
046: */
047: public void processAfterCopy() {
048: this .setStartDateDefault();
049: super .processAfterCopy();
050: }
051:
052: /**
053: * This method will reset AccountDelegate's Start Date to the current timestamp on edits and copies
054: *
055: * @see org.kuali.core.maintenance.KualiMaintainableImpl#processAfterEdit()
056: */
057: public void processAfterEdit() {
058: this .setStartDateDefault();
059: super .processAfterEdit();
060: }
061:
062: /**
063: * This method sets the start date on {@link Delegate} BO
064: */
065: private void setStartDateDefault() {
066: if (this .businessObject != null
067: && this .businessObject instanceof Delegate) {
068: Delegate delegate = (Delegate) this .businessObject;
069: delegate.setAccountDelegateStartDate(SpringContext.getBean(
070: DateTimeService.class).getCurrentTimestamp());
071: }
072: }
073:
074: /**
075: * Generates the appropriate maintenance locks for the {@link Delegate}
076: *
077: * @see org.kuali.core.maintenance.KualiMaintainableImpl#generateMaintenanceLocks()
078: */
079: @Override
080: public List<MaintenanceLock> generateMaintenanceLocks() {
081: Delegate delegate = (Delegate) this .businessObject;
082: List<MaintenanceLock> locks = super .generateMaintenanceLocks();
083: if (delegate.isAccountsDelegatePrmrtIndicator()) {
084: locks.add(createMaintenanceLock(new String[] {
085: "chartOfAccountsCode", "accountNumber",
086: "financialDocumentTypeCode",
087: "accountsDelegatePrmrtIndicator" }));
088: }
089: return locks;
090: }
091:
092: /**
093: * This method creates a maintenance lock for the field names supplied
094: *
095: * @param fieldNames
096: * @return the maintenance lock for supplied field names
097: */
098: private MaintenanceLock createMaintenanceLock(String[] fieldNames) {
099: MaintenanceLock lock = new MaintenanceLock();
100: lock.setDocumentNumber(this .documentNumber);
101: lock
102: .setLockingRepresentation(createLockingRepresentation(fieldNames));
103: return lock;
104:
105: }
106:
107: /**
108: * This method create a locking representation for the field names supplied
109: *
110: * @param fieldNames
111: * @return locking representation string
112: */
113: private String createLockingRepresentation(String[] fieldNames) {
114: StringBuilder lockRepresentation = new StringBuilder();
115:
116: lockRepresentation.append(Delegate.class.getName());
117: lockRepresentation
118: .append(KFSConstants.Maintenance.AFTER_CLASS_DELIM);
119:
120: DataDictionaryService dataDictionaryService = KNSServiceLocator
121: .getDataDictionaryService();
122: EncryptionService encryptionService = KNSServiceLocator
123: .getEncryptionService();
124:
125: int count = 0;
126: for (String fieldName : fieldNames) {
127: lockRepresentation.append(fieldName);
128: lockRepresentation
129: .append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
130: lockRepresentation
131: .append(retrieveFieldValueForLock(fieldName,
132: dataDictionaryService, encryptionService));
133: if (count < (fieldNames.length - 1)) {
134: lockRepresentation
135: .append(KFSConstants.Maintenance.AFTER_VALUE_DELIM);
136: }
137: count += 1;
138: }
139:
140: return lockRepresentation.toString();
141: }
142:
143: /**
144: * This method returns the field value of a given field, converting the value to a String and encrypting it if necessary
145: *
146: * @param fieldName
147: * @param ddService
148: * @return string field value for a lock
149: */
150: private String retrieveFieldValueForLock(String fieldName,
151: DataDictionaryService ddService,
152: EncryptionService encryptionService) {
153: Object fieldValue = ObjectUtils.getPropertyValue(
154: this .businessObject, fieldName);
155: if (fieldValue == null) {
156: fieldValue = "";
157: }
158:
159: // check if field is a secure
160: String displayWorkgroup = ddService
161: .getAttributeDisplayWorkgroup(getBoClass(), fieldName);
162: if (StringUtils.isNotBlank(displayWorkgroup)) {
163: try {
164: fieldValue = encryptionService.encrypt(fieldValue);
165: } catch (GeneralSecurityException e) {
166: LOG
167: .error("Unable to encrypt secure field for locking representation "
168: + e.getMessage());
169: throw new RuntimeException(
170: "Unable to encrypt secure field for locking representation "
171: + e.getMessage());
172: }
173: }
174: return String.valueOf(fieldValue);
175: }
176: }
|