001/*
002 * (C) Copyright 2002-2013 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018package org.nuxeo.ecm.platform.importer.xml.parser;
019
020import java.util.HashMap;
021import java.util.Map;
022
023import org.nuxeo.common.xmap.annotation.XNode;
024import org.nuxeo.common.xmap.annotation.XNodeMap;
025import org.nuxeo.common.xmap.annotation.XObject;
026
027/**
028 * Descriptor that can be used to define how Nuxeo DocumentModel properties are filled from the input XML
029 *
030 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
031 */
032@XObject("attributeConfig")
033public class AttributeConfigDescriptor {
034
035    @XNode("@tagName")
036    protected String tagName;
037
038    @XNode("@docProperty")
039    protected String targetDocProperty;
040
041    // xpath to select when this config may be valid
042    @XNode("@filter")
043    protected String filter;
044
045    // mapping between Nuxeo property names and corresponding xpath to extract
046    // values
047    @XNodeMap(value = "mapping", key = "@documentProperty", type = HashMap.class, componentType = String.class)
048    protected Map<String, String> mapping;
049
050    @XNode("@xmlPath")
051    protected String xmlPath;
052
053    @XNode("@overwrite")
054    protected boolean overwrite = false;
055    
056    public AttributeConfigDescriptor() {
057    }
058
059    public AttributeConfigDescriptor(String tagName, String targetDocProperty, Map<String, String> mapping,
060            String filter) {
061        this.tagName = tagName;
062        this.targetDocProperty = targetDocProperty;
063        if (mapping == null) {
064            mapping = new HashMap<String, String>();
065        } else {
066            this.mapping = mapping;
067        }
068        this.filter = filter;
069    }
070
071    public AttributeConfigDescriptor(String tagName, String targetDocProperty, String xmlPath, String filter) {
072        this.tagName = tagName;
073        this.targetDocProperty = targetDocProperty;
074        this.xmlPath = xmlPath;
075        this.filter = filter;
076    }
077
078    public String getTagName() {
079        return tagName;
080    }
081
082    public String getTargetDocProperty() {
083        return targetDocProperty;
084    }
085
086    public String getFilter() {
087        return filter;
088    }
089
090    public Map<String, String> getMapping() {
091        return mapping;
092    }
093
094    public String getSingleXpath() {
095        if (xmlPath != null) {
096            return xmlPath;
097        }
098        if (mapping != null && !mapping.keySet().isEmpty()) {
099            return mapping.values().iterator().next();
100        }
101        return null;
102    }
103
104    public boolean getOverwrite(){
105        return overwrite;
106    }
107
108    @Override
109    public String toString() {
110        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";
111        String result = String.format(msg, tagName, targetDocProperty, filter, xmlPath, overwrite);
112        if (mapping != null && !mapping.keySet().isEmpty()) {
113            for (String key : mapping.keySet()) {
114                result += "\t\t" + key + ": " + mapping.get(key) + "\n";
115            }
116        } else {
117            result += "\t\tNO MAPPING\n";
118        }
119
120        return result;
121    }
122}