001/**
002 * Copyright 2005-2017 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 */
016package org.kuali.rice.krms.ojb;
017
018import org.apache.ojb.broker.accesslayer.conversions.ConversionException;
019import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
020import org.kuali.rice.krms.api.repository.typerelation.RelationshipType;
021
022/**
023 * This class performs an OJB conversion between a String and the code value in the RelationType enum.
024 *
025 * @author Kuali Student Team
026 */
027public class OjbRelationshipTypeStringConversion implements FieldConversion {
028
029    @Override
030    public Object javaToSql(Object source) throws ConversionException {
031        if (source instanceof RelationshipType) {
032            if (source != null) {
033                RelationshipType relationshipType = (RelationshipType) source;
034                return relationshipType.getCode();
035            }
036        }
037        return null;
038    }
039
040    @Override
041    public Object sqlToJava(Object source) throws ConversionException {
042        if (source instanceof String) {
043            if (source != null) {
044                String s = (String) source;
045                for (RelationshipType relType : RelationshipType.values()) {
046                    if (relType.getCode().equals(s)) {
047                        return relType;
048                    }
049                }
050            }
051        }
052        return null;
053    }
054}