001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.core.framework.persistence.ojb.conversion;
017    
018    import java.security.GeneralSecurityException;
019    
020    import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
021    import org.kuali.rice.core.api.CoreApiServiceLocator;
022    
023    /**
024     * This class calls core service to encrypt values going to the database and decrypt values coming back from the database.
025     * 
026     * 
027     */
028    
029    public class OjbKualiEncryptDecryptFieldConversion implements FieldConversion {
030        private static final long serialVersionUID = 2450111778124335242L;
031    
032        /**
033         * @see FieldConversion#javaToSql(Object)
034         */
035        public Object javaToSql(Object source) {
036            Object converted = source;
037    
038            try {
039                    //check if the encryption service is enable before using it
040                    if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
041                    converted = CoreApiServiceLocator.getEncryptionService().encrypt(converted);
042                    }
043            }
044            catch (GeneralSecurityException e) {
045                throw new RuntimeException("Unable to encrypt value to db: " + e.getMessage());
046            }
047    
048            return converted;
049        }
050    
051        /**
052         * @see FieldConversion#sqlToJava(Object)
053         */
054        public Object sqlToJava(Object source) {
055            String converted = "";
056            if (source != null) {
057                converted = source.toString();
058            }
059    
060            try {
061                    //check if the encryption service is enable before using it
062                    if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
063                    converted = CoreApiServiceLocator.getEncryptionService().decrypt(converted);
064                    }
065            }
066            catch (GeneralSecurityException e) {
067                throw new RuntimeException("Unable to decrypt value from db: " + e.getMessage());
068            }
069    
070            return converted;
071        }
072    }