1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | 0 | 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 | 0 | checkArguments(businessObjectClass, attributeNames, true); |
39 | 0 | } |
40 | |
|
41 | |
@Override |
42 | |
public void checkArguments(Class<? extends PersistableBusinessObject> businessObjectClass, Set<String> attributeNames, boolean checkOjbEncryptConfig) { |
43 | 0 | if ((businessObjectClass == null) || (attributeNames == null)) { |
44 | 0 | 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 | 0 | classDescriptor = getClassDescriptor(businessObjectClass); |
50 | 0 | } catch (ClassNotPersistableException e) { |
51 | 0 | 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 | 0 | } |
55 | 0 | for (String attributeName : attributeNames) { |
56 | 0 | if (classDescriptor.getFieldDescriptorByName(attributeName) == null) { |
57 | 0 | 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 | 0 | if (checkOjbEncryptConfig && !(classDescriptor.getFieldDescriptorByName(attributeName).getFieldConversion() instanceof OjbKualiEncryptDecryptFieldConversion)) { |
65 | 0 | 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 | 0 | } |
76 | |
|
77 | |
@Override |
78 | |
public void createBackupTable(Class<? extends PersistableBusinessObject> businessObjectClass) { |
79 | 0 | postDataLoadEncryptionDao.createBackupTable(getClassDescriptor(businessObjectClass).getFullTableName()); |
80 | 0 | } |
81 | |
|
82 | |
@Override |
83 | |
public void prepClassDescriptor(Class<? extends PersistableBusinessObject> businessObjectClass, Set<String> attributeNames) { |
84 | 0 | ClassDescriptor classDescriptor = getClassDescriptor(businessObjectClass); |
85 | 0 | for (String attributeName : attributeNames) { |
86 | 0 | classDescriptor.getFieldDescriptorByName(attributeName).setFieldConversionClassName( |
87 | |
FieldConversionDefaultImpl.class.getName()); |
88 | |
} |
89 | 0 | } |
90 | |
|
91 | |
@Override |
92 | |
public void truncateTable(Class<? extends PersistableBusinessObject> businessObjectClass) { |
93 | 0 | postDataLoadEncryptionDao.truncateTable(getClassDescriptor(businessObjectClass).getFullTableName()); |
94 | 0 | } |
95 | |
|
96 | |
@Override |
97 | |
public void encrypt(PersistableBusinessObject businessObject, Set<String> attributeNames) { |
98 | 0 | for (String attributeName : attributeNames) { |
99 | |
try { |
100 | 0 | PropertyUtils.setProperty(businessObject, attributeName, encryptionService.encrypt(PropertyUtils |
101 | |
.getProperty(businessObject, attributeName))); |
102 | 0 | } catch (Exception e) { |
103 | 0 | 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 | 0 | } |
107 | |
} |
108 | 0 | businessObjectService.save(businessObject); |
109 | 0 | } |
110 | |
|
111 | |
@Override |
112 | |
public void restoreClassDescriptor(Class<? extends PersistableBusinessObject> businessObjectClass, Set<String> attributeNames) { |
113 | 0 | ClassDescriptor classDescriptor = getClassDescriptor(businessObjectClass); |
114 | 0 | for (String attributeName : attributeNames) { |
115 | 0 | classDescriptor.getFieldDescriptorByName(attributeName).setFieldConversionClassName( |
116 | |
OjbKualiEncryptDecryptFieldConversion.class.getName()); |
117 | |
} |
118 | 0 | businessObjectService.countMatching(businessObjectClass, Collections.<String, Object>emptyMap()); |
119 | 0 | } |
120 | |
|
121 | |
@Override |
122 | |
public void restoreTableFromBackup(Class<? extends PersistableBusinessObject> businessObjectClass) { |
123 | 0 | postDataLoadEncryptionDao.restoreTableFromBackup(getClassDescriptor(businessObjectClass).getFullTableName()); |
124 | 0 | } |
125 | |
|
126 | |
@Override |
127 | |
public void dropBackupTable(Class<? extends PersistableBusinessObject> businessObjectClass) { |
128 | 0 | postDataLoadEncryptionDao.dropBackupTable(getClassDescriptor(businessObjectClass).getFullTableName()); |
129 | 0 | } |
130 | |
|
131 | |
|
132 | |
public void setPostDataLoadEncryptionDao(PostDataLoadEncryptionDao postDataLoadEncryptionDao) { |
133 | 0 | this.postDataLoadEncryptionDao = postDataLoadEncryptionDao; |
134 | 0 | } |
135 | |
|
136 | |
public void setEncryptionService(EncryptionService encryptionService) { |
137 | 0 | this.encryptionService = encryptionService; |
138 | 0 | } |
139 | |
|
140 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
141 | 0 | this.businessObjectService = businessObjectService; |
142 | 0 | } |
143 | |
} |