001/*
002 * (C) Copyright 2006-2007 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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 *
019 * $Id: AbstractIOResourceAdapter.java 25080 2007-09-18 14:52:20Z atchertchian $
020 */
021
022package org.nuxeo.ecm.platform.io.api;
023
024import java.io.InputStream;
025import java.io.OutputStream;
026import java.io.Serializable;
027import java.util.Collection;
028import java.util.HashMap;
029import java.util.Map;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.nuxeo.ecm.core.api.DocumentRef;
034import org.nuxeo.ecm.core.io.DocumentTranslationMap;
035
036/**
037 * Abstract implementation for {@link IOResourceAdapter}.
038 * <p>
039 * Offers helper methods for properties.
040 *
041 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
042 */
043public abstract class AbstractIOResourceAdapter implements IOResourceAdapter {
044
045    private static final long serialVersionUID = 4399167777434048174L;
046
047    private static final Log log = LogFactory.getLog(AbstractIOResourceAdapter.class);
048
049    protected Map<String, Serializable> properties;
050
051    protected AbstractIOResourceAdapter() {
052        properties = new HashMap<>();
053    }
054
055    @Override
056    public Map<String, Serializable> getProperties() {
057        return properties;
058    }
059
060    @Override
061    public void setProperties(Map<String, Serializable> properties) {
062        this.properties = properties;
063    }
064
065    @Override
066    public abstract IOResources extractResources(String repo, Collection<DocumentRef> sources);
067
068    @Override
069    public abstract IOResources translateResources(String repo, IOResources resources, DocumentTranslationMap map);
070
071    @Override
072    public abstract void storeResources(IOResources newResources);
073
074    @Override
075    public abstract void getResourcesAsXML(OutputStream out, IOResources newResources);
076
077    @Override
078    public abstract IOResources loadResourcesFromXML(InputStream stream);
079
080    // helper methods
081
082    protected boolean getBooleanProperty(String propName) {
083        Boolean value = (Boolean) properties.get(propName);
084        if (value == null) {
085            return false;
086        }
087        return value;
088    }
089
090    protected void setBooleanProperty(String propName, Serializable propValue) {
091        if (propValue == null) {
092            return;
093        }
094        if (propValue instanceof String) {
095            properties.put(propName, Boolean.valueOf((String) propValue));
096        } else {
097            log.error(String.format("Property %s needs a string representing a boolean:" + " invalid value %s",
098                    propName, propValue));
099        }
100    }
101
102    protected String getStringProperty(String propName) {
103        return (String) properties.get(propName);
104    }
105
106    protected void setStringProperty(String propName, Serializable propValue) {
107        if (propValue == null) {
108            return;
109        }
110        if (!(propValue instanceof String)) {
111            log.error(String.format("Property %s needs a string value:" + " invalid value %s", propName, propValue));
112        }
113        properties.put(propName, propValue);
114    }
115
116    protected String[] getStringArrayProperty(String propName) {
117        return (String[]) properties.get(propName);
118    }
119
120    protected void setStringArrayProperty(String propName, Serializable propValue) {
121        if (propValue == null) {
122            return;
123        }
124        if (!(propValue instanceof String[])) {
125            log.error(String.format("Property %s needs a string array, invalid value %s", propName, propValue));
126        }
127        properties.put(propName, propValue);
128    }
129
130}