001/**
002 * Copyright 2005-2015 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.kns.datadictionary.exporter;
017
018import org.kuali.rice.krad.datadictionary.DataDictionaryEntryBase;
019import org.kuali.rice.krad.datadictionary.PrimitiveAttributeDefinition;
020import org.kuali.rice.krad.datadictionary.RelationshipDefinition;
021import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
022import org.kuali.rice.krad.util.LegacyUtils;
023
024/**
025 * RelationshipsMapBuilder
026 * 
027 * @deprecated Only used by KNS classes, no replacement.
028 */
029@Deprecated
030public class RelationshipsMapBuilder {
031
032    /**
033     * Default constructor
034     */
035    public RelationshipsMapBuilder() {
036    }
037
038
039    /**
040     * @param entry
041     * @return ExportMap containing the standard entries for the entry's RelationshipDefinitions
042     */
043    public ExportMap buildRelationshipsMap(DataDictionaryEntryBase entry) {
044        ExportMap relationshipsMap = new ExportMap("relationships");
045
046        for ( RelationshipDefinition relationship : entry.getRelationships() ) {
047            //if in legacy context do not use any of the metadata injected stuff
048            //this prevents a problem with duplicate entries that exists
049            if(!(relationship.hasEmbeddedDataObjectMetadata() && LegacyUtils.isInLegacyContext())){
050                relationshipsMap.set(buildRelationshipMap(relationship));
051            }
052        }
053
054        return relationshipsMap;
055    }
056
057    private ExportMap buildRelationshipMap(RelationshipDefinition relationship) {
058        ExportMap relationshipMap = new ExportMap(relationship.getObjectAttributeName());
059
060        ExportMap attributesMap = new ExportMap("primitiveAttributes");
061
062        int count = 0;
063        for (PrimitiveAttributeDefinition primitiveAttributeDefinition : relationship.getPrimitiveAttributes()) {
064            ExportMap attributeMap = new ExportMap(Integer.toString(count++));
065            attributeMap.set("sourceName", primitiveAttributeDefinition.getSourceName());
066            attributeMap.set("targetName", primitiveAttributeDefinition.getTargetName());
067
068            attributesMap.set(attributeMap);
069        }
070
071        relationshipMap.set(attributesMap);
072
073        return relationshipMap;
074    }
075
076}