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