001/*
002 * (C) Copyright 2006-2011 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 *     Bogdan Stefanescu
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.core.api.model;
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.Collection;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.nuxeo.ecm.core.api.PropertyException;
029import org.nuxeo.ecm.core.api.model.impl.ListProperty;
030import org.nuxeo.ecm.core.api.model.impl.MapProperty;
031import org.nuxeo.ecm.core.api.model.impl.ScalarProperty;
032import org.nuxeo.ecm.core.api.model.impl.primitives.BlobProperty;
033import org.nuxeo.ecm.core.schema.types.QName;
034
035/**
036 * Exporter for a document's values into a map.
037 * <p>
038 * The values of the first-level keys of the map may be prefixed (standard prefix:name naming) or not.
039 */
040@SuppressWarnings("unchecked")
041public class ValueExporter implements PropertyVisitor {
042
043    private final Map<String, Serializable> result = new HashMap<>();
044
045    private final boolean prefixed;
046
047    /**
048     * Constructs an exporter.
049     *
050     * @param prefixed whether first-level keys of the map are prefixed
051     */
052    public ValueExporter(boolean prefixed) {
053        this.prefixed = prefixed;
054    }
055
056    public Map<String, Serializable> getResult() {
057        return result;
058    }
059
060    public Map<String, Serializable> run(DocumentPart dp) throws PropertyException {
061        dp.accept(this, result);
062        return result;
063    }
064
065    protected String getName(Property property) {
066        QName name = property.getField().getName();
067        return prefixed ? name.getPrefixedName() : name.getLocalName();
068    }
069
070    @Override
071    public boolean acceptPhantoms() {
072        return false;
073    }
074
075    @Override
076    public Object visit(MapProperty property, Object arg) throws PropertyException {
077
078        Serializable value;
079        if (property.isContainer()) {
080            value = new HashMap<String, Serializable>();
081        } else {
082            value = property.getValue();
083        }
084
085        if (BlobProperty.class.isAssignableFrom(property.getClass())) {
086            value = property.getValue();
087            if (property.getParent().isList()) {
088                ((Collection<Serializable>) arg).add(value);
089            } else {
090                ((Map<String, Serializable>) arg).put(getName(property), value);
091            }
092            return null;
093        } else if (property.getParent().isList()) {
094            ((Collection<Serializable>) arg).add(value);
095        } else {
096            ((Map<String, Serializable>) arg).put(getName(property), value);
097        }
098        return value;
099    }
100
101    @Override
102    public Object visit(ListProperty property, Object arg) throws PropertyException {
103        Serializable value;
104        if (property.isContainer()) {
105            value = new ArrayList<Serializable>();
106        } else {
107            value = property.getValue();
108        }
109        if (property.getParent().isList()) {
110            ((Collection<Serializable>) arg).add(value);
111        } else {
112            ((Map<String, Serializable>) arg).put(getName(property), value);
113        }
114        return value;
115    }
116
117    @Override
118    public Object visit(ScalarProperty property, Object arg) throws PropertyException {
119        Serializable value = property.getValue();
120        if (property.getParent().isList()) {
121            ((Collection<Serializable>) arg).add(value);
122        } else {
123            ((Map<String, Serializable>) arg).put(getName(property), value);
124        }
125        return null;
126    }
127
128}