1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.data.jpa.converters;
17
18 import javax.persistence.AttributeConverter;
19 import javax.persistence.Converter;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.kuali.rice.core.api.CoreApiServiceLocator;
23
24
25
26
27
28
29 @Converter
30 public class EncryptionConverter implements AttributeConverter<String, String> {
31
32
33
34
35
36
37 @Override
38 public String convertToDatabaseColumn(String objectValue) {
39
40 if (objectValue == null) {
41 return null;
42 }
43 if (StringUtils.isEmpty(objectValue.toString())) {
44 return "";
45 }
46 try {
47
48 if (CoreApiServiceLocator.getEncryptionService().isEnabled()) {
49 return CoreApiServiceLocator.getEncryptionService().encrypt(objectValue);
50 }
51 } catch (Exception e) {
52 throw new RuntimeException("Exception while attempting to encrypt value for DB: ", e);
53 }
54 return objectValue;
55 }
56
57
58
59
60
61
62 @Override
63 public String convertToEntityAttribute(String dataValue) {
64
65 if (dataValue == null) {
66 return null;
67 }
68 if (StringUtils.isEmpty(dataValue.toString())) {
69 return "";
70 }
71 try {
72
73 if (CoreApiServiceLocator.getEncryptionService().isEnabled()) {
74 return CoreApiServiceLocator.getEncryptionService().decrypt(dataValue.toString());
75 }
76 } catch (Exception e) {
77 throw new RuntimeException("Exception while attempting to decrypt value from DB: ", e);
78 }
79 return dataValue;
80 }
81
82 }