View Javadoc

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