001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License"); you may not use this file except in
005: * compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.opensource.org/licenses/ecl1.php
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
010: * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
011: * language governing permissions and limitations under the License.
012: */
013: package org.kuali.core.service.impl;
014:
015: import java.util.HashMap;
016: import java.util.Set;
017:
018: import org.apache.commons.beanutils.PropertyUtils;
019: import org.apache.ojb.broker.accesslayer.conversions.FieldConversionDefaultImpl;
020: import org.apache.ojb.broker.metadata.ClassDescriptor;
021: import org.kuali.core.bo.PersistableBusinessObject;
022: import org.kuali.core.dao.PostDataLoadEncryptionDao;
023: import org.kuali.core.exceptions.ClassNotPersistableException;
024: import org.kuali.core.service.BusinessObjectService;
025: import org.kuali.core.service.EncryptionService;
026: import org.kuali.core.service.PostDataLoadEncryptionService;
027: import org.kuali.core.util.OjbKualiEncryptDecryptFieldConversion;
028: import org.springframework.transaction.annotation.Transactional;
029:
030: @Transactional
031: public class PostDataLoadEncryptionServiceImpl extends
032: PersistenceServiceImplBase implements
033: PostDataLoadEncryptionService {
034: private BusinessObjectService businessObjectService;
035: private EncryptionService encryptionService;
036: private PostDataLoadEncryptionDao postDataLoadEncryptionDao;
037:
038: public void checkArguments(Class businessObjectClass,
039: Set<String> attributeNames) {
040: if ((businessObjectClass == null) || (attributeNames == null)) {
041: throw new IllegalArgumentException(
042: "PostDataLoadEncryptionServiceImpl.encrypt does not allow a null business object Class or attributeNames Set");
043: }
044: ClassDescriptor classDescriptor = null;
045: try {
046: classDescriptor = getClassDescriptor(businessObjectClass);
047: } catch (ClassNotPersistableException e) {
048: throw new IllegalArgumentException(
049: "PostDataLoadEncryptionServiceImpl.encrypt does not handle business object classes that do not have a corresponding ClassDescriptor defined in the OJB repository",
050: e);
051: }
052: for (String attributeName : attributeNames) {
053: if (classDescriptor.getFieldDescriptorByName(attributeName) == null) {
054: throw new IllegalArgumentException(
055: new StringBuffer("Attribute ")
056: .append(attributeName)
057: .append(
058: " specified to PostDataLoadEncryptionServiceImpl.encrypt is not in the OJB repository ClassDescriptor for Class ")
059: .append(businessObjectClass).toString());
060: }
061: if (!(classDescriptor.getFieldDescriptorByName(
062: attributeName).getFieldConversion() instanceof OjbKualiEncryptDecryptFieldConversion)) {
063: throw new IllegalArgumentException(
064: new StringBuffer("Attribute ")
065: .append(attributeName)
066: .append(" of business object Class ")
067: .append(businessObjectClass)
068: .append(
069: " specified to PostDataLoadEncryptionServiceImpl.encrypt is not configured for encryption in the OJB repository")
070: .toString());
071: }
072: }
073: }
074:
075: public void createBackupTable(Class businessObjectClass) {
076: postDataLoadEncryptionDao.createBackupTable(getClassDescriptor(
077: businessObjectClass).getFullTableName());
078: }
079:
080: public void prepClassDescriptor(Class businessObjectClass,
081: Set<String> attributeNames) {
082: ClassDescriptor classDescriptor = getClassDescriptor(businessObjectClass);
083: for (String attributeName : attributeNames) {
084: classDescriptor.getFieldDescriptorByName(attributeName)
085: .setFieldConversionClassName(
086: FieldConversionDefaultImpl.class.getName());
087: }
088: }
089:
090: public void truncateTable(Class businessObjectClass) {
091: postDataLoadEncryptionDao.truncateTable(getClassDescriptor(
092: businessObjectClass).getFullTableName());
093: }
094:
095: public void encrypt(PersistableBusinessObject businessObject,
096: Set<String> attributeNames) {
097: for (String attributeName : attributeNames) {
098: try {
099: PropertyUtils
100: .setProperty(businessObject, attributeName,
101: encryptionService.encrypt(PropertyUtils
102: .getProperty(businessObject,
103: attributeName)));
104: } catch (Exception e) {
105: throw new RuntimeException(
106: new StringBuffer(
107: "PostDataLoadEncryptionServiceImpl caught exception while attempting to encrypt attribute ")
108: .append(attributeName).append(
109: " of Class ").append(
110: businessObject.getClass())
111: .toString(), e);
112: }
113: }
114: businessObjectService.save(businessObject);
115: }
116:
117: public void restoreClassDescriptor(Class businessObjectClass,
118: Set<String> attributeNames) {
119: ClassDescriptor classDescriptor = getClassDescriptor(businessObjectClass);
120: for (String attributeName : attributeNames) {
121: classDescriptor.getFieldDescriptorByName(attributeName)
122: .setFieldConversionClassName(
123: OjbKualiEncryptDecryptFieldConversion.class
124: .getName());
125: }
126: businessObjectService.countMatching(businessObjectClass,
127: new HashMap());
128: }
129:
130: public void restoreTableFromBackup(Class businessObjectClass) {
131: postDataLoadEncryptionDao
132: .restoreTableFromBackup(getClassDescriptor(
133: businessObjectClass).getFullTableName());
134: }
135:
136: public void dropBackupTable(Class businessObjectClass) {
137: postDataLoadEncryptionDao.dropBackupTable(getClassDescriptor(
138: businessObjectClass).getFullTableName());
139: }
140:
141: public void setPostDataLoadEncryptionDao(
142: PostDataLoadEncryptionDao postDataLoadEncryptionDao) {
143: this .postDataLoadEncryptionDao = postDataLoadEncryptionDao;
144: }
145:
146: public void setEncryptionService(EncryptionService encryptionService) {
147: this .encryptionService = encryptionService;
148: }
149:
150: public void setBusinessObjectService(
151: BusinessObjectService businessObjectService) {
152: this.businessObjectService = businessObjectService;
153: }
154: }
|