001    /*
002     * Copyright 2011 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.kfs.sys.spring.datadictionary;
017    
018    import java.util.ArrayList;
019    
020    import org.kuali.rice.kns.datadictionary.PrimitiveAttributeDefinition;
021    import org.kuali.rice.kns.datadictionary.SupportAttributeDefinition;
022    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
023    import org.springframework.beans.factory.xml.ParserContext;
024    import org.springframework.util.StringUtils;
025    import org.w3c.dom.Element;
026    import org.w3c.dom.NamedNodeMap;
027    import org.w3c.dom.Node;
028    import org.w3c.dom.NodeList;
029    
030    public class RelationshipBeanDefinitionParser extends KualiBeanDefinitionParserBase {
031    
032        @Override
033        protected String getBaseBeanTypeParent(Element element) {
034            return "RelationshipDefinition";
035        }
036        
037        @Override
038        protected void doParse(Element element, ParserContext context, BeanDefinitionBuilder bean) {
039            // get all attributes
040            String objectAttribute = element.getAttribute("objectAttribute");
041            String targetClass = element.getAttribute("targetClass");
042    
043            // now, set on the bean definition
044            if ( StringUtils.hasText(objectAttribute) ) {
045                bean.addPropertyValue("objectAttributeName", objectAttribute);
046            }
047            if ( StringUtils.hasText(targetClass) ) {
048                bean.addPropertyValue("targetClass", targetClass);
049            }
050            
051            NodeList children = element.getChildNodes();
052            ArrayList<PrimitiveAttributeDefinition> pDefs = new ArrayList<PrimitiveAttributeDefinition>();
053            ArrayList<SupportAttributeDefinition> sDefs = new ArrayList<SupportAttributeDefinition>();
054            
055            for ( int i = 0; i < children.getLength(); i++ ) {
056                Node child = children.item(i);
057                String nodeName = child.getLocalName();
058                if ( nodeName == null ) continue;
059                
060                if ( nodeName.equals("primitiveAttribute") ) {
061                    NamedNodeMap attributes = child.getAttributes();
062                    String source = attributes.getNamedItem("source").getNodeValue();
063                    String target = attributes.getNamedItem("target").getNodeValue();
064                    PrimitiveAttributeDefinition pad = new PrimitiveAttributeDefinition();
065                    pad.setSourceName(source);
066                    pad.setTargetName(target);
067                    pDefs.add(pad);
068                } else if ( nodeName.equals("supportAttribute") ) {
069                    NamedNodeMap attributes = child.getAttributes();
070                    String source = attributes.getNamedItem("source").getNodeValue();
071                    String target = attributes.getNamedItem("target").getNodeValue();
072                    boolean identifier = false;
073                    String identifierStr = attributes.getNamedItem("identifier").getNodeValue();
074                    if ( identifierStr != null ) {
075                        identifier = Boolean.valueOf( identifierStr );
076                    }
077                    SupportAttributeDefinition pad = new SupportAttributeDefinition();
078                    pad.setSourceName(source);
079                    pad.setTargetName(target);
080                    pad.setIdentifier(identifier);
081                    sDefs.add(pad);
082                }
083                bean.addPropertyValue("primitiveAttributes", pDefs);
084                bean.addPropertyValue("supportAttributes", sDefs);
085            }
086        }
087    
088        
089    }