View Javadoc

1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in
5    * compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.opensource.org/licenses/ecl2.php
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
10   * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
11   * language governing permissions and limitations under the License.
12   */
13  package org.kuali.rice.kns.service.impl;
14  
15  import java.util.HashMap;
16  import java.util.Set;
17  
18  import org.apache.commons.beanutils.PropertyUtils;
19  import org.apache.ojb.broker.accesslayer.conversions.FieldConversionDefaultImpl;
20  import org.apache.ojb.broker.metadata.ClassDescriptor;
21  import org.kuali.rice.core.service.EncryptionService;
22  import org.kuali.rice.kns.bo.PersistableBusinessObject;
23  import org.kuali.rice.kns.dao.PostDataLoadEncryptionDao;
24  import org.kuali.rice.kns.exception.ClassNotPersistableException;
25  import org.kuali.rice.kns.service.BusinessObjectService;
26  import org.kuali.rice.kns.service.PostDataLoadEncryptionService;
27  import org.kuali.rice.kns.util.OjbKualiEncryptDecryptFieldConversion;
28  
29  public class PostDataLoadEncryptionServiceImpl extends PersistenceServiceImplBase implements PostDataLoadEncryptionService {
30      private BusinessObjectService businessObjectService;
31      private EncryptionService encryptionService;
32      private PostDataLoadEncryptionDao postDataLoadEncryptionDao;
33  
34      public void checkArguments(Class businessObjectClass, Set<String> attributeNames) {
35      	checkArguments(businessObjectClass, attributeNames, true);
36      }
37      
38      public void checkArguments(Class businessObjectClass, Set<String> attributeNames, boolean checkOjbEncryptConfig) {
39  	if ((businessObjectClass == null) || (attributeNames == null)) {
40  	    throw new IllegalArgumentException(
41  		    "PostDataLoadEncryptionServiceImpl.encrypt does not allow a null business object Class or attributeNames Set");
42  	}
43  	ClassDescriptor classDescriptor = null;
44  	try {
45  	    classDescriptor = getClassDescriptor(businessObjectClass);
46  	} catch (ClassNotPersistableException e) {
47  	    throw new IllegalArgumentException(
48  		    "PostDataLoadEncryptionServiceImpl.encrypt does not handle business object classes that do not have a corresponding ClassDescriptor defined in the OJB repository",
49  		    e);
50  	}
51  	for (String attributeName : attributeNames) {
52  	    if (classDescriptor.getFieldDescriptorByName(attributeName) == null) {
53  		throw new IllegalArgumentException(
54  			new StringBuffer("Attribute ")
55  				.append(attributeName)
56  				.append(
57  					" specified to PostDataLoadEncryptionServiceImpl.encrypt is not in the OJB repository ClassDescriptor for Class ")
58  				.append(businessObjectClass).toString());
59  	    }
60  	    if (checkOjbEncryptConfig && !(classDescriptor.getFieldDescriptorByName(attributeName).getFieldConversion() instanceof OjbKualiEncryptDecryptFieldConversion)) {
61  		throw new IllegalArgumentException(
62  			new StringBuffer("Attribute ")
63  				.append(attributeName)
64  				.append(" of business object Class ")
65  				.append(businessObjectClass)
66  				.append(
67  					" specified to PostDataLoadEncryptionServiceImpl.encrypt is not configured for encryption in the OJB repository")
68  				.toString());
69  	    }
70  	}
71      }
72  
73      public void createBackupTable(Class businessObjectClass) {
74  	postDataLoadEncryptionDao.createBackupTable(getClassDescriptor(businessObjectClass).getFullTableName());
75      }
76  
77      public void prepClassDescriptor(Class businessObjectClass, Set<String> attributeNames) {
78  	ClassDescriptor classDescriptor = getClassDescriptor(businessObjectClass);
79  	for (String attributeName : attributeNames) {
80  	    classDescriptor.getFieldDescriptorByName(attributeName).setFieldConversionClassName(
81  		    FieldConversionDefaultImpl.class.getName());
82  	}
83      }
84  
85      public void truncateTable(Class businessObjectClass) {
86  	postDataLoadEncryptionDao.truncateTable(getClassDescriptor(businessObjectClass).getFullTableName());
87      }
88  
89      public void encrypt(PersistableBusinessObject businessObject, Set<String> attributeNames) {
90  	for (String attributeName : attributeNames) {
91  	    try {
92  		PropertyUtils.setProperty(businessObject, attributeName, encryptionService.encrypt(PropertyUtils
93  			.getProperty(businessObject, attributeName)));
94  	    } catch (Exception e) {
95  		throw new RuntimeException(new StringBuffer(
96  			"PostDataLoadEncryptionServiceImpl caught exception while attempting to encrypt attribute ").append(
97  			attributeName).append(" of Class ").append(businessObject.getClass()).toString(), e);
98  	    }
99  	}
100 	businessObjectService.save(businessObject);
101     }
102 
103     public void restoreClassDescriptor(Class businessObjectClass, Set<String> attributeNames) {
104 	ClassDescriptor classDescriptor = getClassDescriptor(businessObjectClass);
105 	for (String attributeName : attributeNames) {
106 	    classDescriptor.getFieldDescriptorByName(attributeName).setFieldConversionClassName(
107 		    OjbKualiEncryptDecryptFieldConversion.class.getName());
108 	}
109 	businessObjectService.countMatching(businessObjectClass, new HashMap());
110     }
111 
112     public void restoreTableFromBackup(Class businessObjectClass) {
113 	postDataLoadEncryptionDao.restoreTableFromBackup(getClassDescriptor(businessObjectClass).getFullTableName());
114     }
115 
116     public void dropBackupTable(Class businessObjectClass) {
117 	postDataLoadEncryptionDao.dropBackupTable(getClassDescriptor(businessObjectClass).getFullTableName());
118     }
119 
120     public void setPostDataLoadEncryptionDao(PostDataLoadEncryptionDao postDataLoadEncryptionDao) {
121 	this.postDataLoadEncryptionDao = postDataLoadEncryptionDao;
122     }
123 
124     public void setEncryptionService(EncryptionService encryptionService) {
125 	this.encryptionService = encryptionService;
126     }
127 
128     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
129 	this.businessObjectService = businessObjectService;
130     }
131 }