001/*
002 * (C) Copyright 2006-2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020package org.nuxeo.ecm.platform.importer.xml.parser;
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XNodeMap;
027import org.nuxeo.common.xmap.annotation.XObject;
028
029/**
030 * Descriptor that can be used to define how Nuxeo DocumentModel properties are filled from the input XML
031 *
032 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
033 */
034@XObject("attributeConfig")
035public class AttributeConfigDescriptor {
036
037    @XNode("@tagName")
038    protected String tagName;
039
040    @XNode("@docProperty")
041    protected String targetDocProperty;
042
043    // xpath to select when this config may be valid
044    @XNode("@filter")
045    protected String filter;
046
047    // mapping between Nuxeo property names and corresponding xpath to extract
048    // values
049    @XNodeMap(value = "mapping", key = "@documentProperty", type = HashMap.class, componentType = String.class)
050    protected Map<String, String> mapping;
051
052    @XNode("@xmlPath")
053    protected String xmlPath;
054
055    @XNode("@overwrite")
056    protected boolean overwrite = false;
057    
058    public AttributeConfigDescriptor() {
059    }
060
061    public AttributeConfigDescriptor(String tagName, String targetDocProperty, Map<String, String> mapping,
062            String filter) {
063        this.tagName = tagName;
064        this.targetDocProperty = targetDocProperty;
065        if (mapping == null) {
066            mapping = new HashMap<String, String>();
067        } else {
068            this.mapping = mapping;
069        }
070        this.filter = filter;
071    }
072
073    public AttributeConfigDescriptor(String tagName, String targetDocProperty, String xmlPath, String filter) {
074        this.tagName = tagName;
075        this.targetDocProperty = targetDocProperty;
076        this.xmlPath = xmlPath;
077        this.filter = filter;
078    }
079
080    public String getTagName() {
081        return tagName;
082    }
083
084    public String getTargetDocProperty() {
085        return targetDocProperty;
086    }
087
088    public String getFilter() {
089        return filter;
090    }
091
092    public Map<String, String> getMapping() {
093        return mapping;
094    }
095
096    public String getSingleXpath() {
097        if (xmlPath != null) {
098            return xmlPath;
099        }
100        if (mapping != null && !mapping.keySet().isEmpty()) {
101            return mapping.values().iterator().next();
102        }
103        return null;
104    }
105
106    public boolean getOverwrite(){
107        return overwrite;
108    }
109
110    @Override
111    public String toString() {
112        String msg = "AttributeConfig\n\tTag Name: %s\n\tTarget Doc Property: %s\n\tFilter %s\n\tXML Path: %s\n\tOverwrite if list: %s\n\tMapping:\n";
113        String result = String.format(msg, tagName, targetDocProperty, filter, xmlPath, overwrite);
114        if (mapping != null && !mapping.keySet().isEmpty()) {
115            for (String key : mapping.keySet()) {
116                result += "\t\t" + key + ": " + mapping.get(key) + "\n";
117            }
118        } else {
119            result += "\t\tNO MAPPING\n";
120        }
121
122        return result;
123    }
124}