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<String, Serializable>();
053    }
054
055    public Map<String, Serializable> getProperties() {
056        return properties;
057    }
058
059    public void setProperties(Map<String, Serializable> properties) {
060        this.properties = properties;
061    }
062
063    public abstract IOResources extractResources(String repo, Collection<DocumentRef> sources);
064
065    public abstract IOResources translateResources(String repo, IOResources resources, DocumentTranslationMap map);
066
067    public abstract void storeResources(IOResources newResources);
068
069    public abstract void getResourcesAsXML(OutputStream out, IOResources newResources);
070
071    public abstract IOResources loadResourcesFromXML(InputStream stream);
072
073    // helper methods
074
075    protected boolean getBooleanProperty(String propName) {
076        Boolean value = (Boolean) properties.get(propName);
077        if (value == null) {
078            return false;
079        }
080        return value;
081    }
082
083    protected void setBooleanProperty(String propName, Serializable propValue) {
084        if (propValue == null) {
085            return;
086        }
087        if (propValue instanceof String) {
088            properties.put(propName, Boolean.valueOf((String) propValue));
089        } else {
090            log.error(String.format("Property %s needs a string representing a boolean:" + " invalid value %s",
091                    propName, propValue));
092        }
093    }
094
095    protected String getStringProperty(String propName) {
096        return (String) properties.get(propName);
097    }
098
099    protected void setStringProperty(String propName, Serializable propValue) {
100        if (propValue == null) {
101            return;
102        }
103        if (!(propValue instanceof String)) {
104            log.error(String.format("Property %s needs a string value:" + " invalid value %s", propName, propValue));
105        }
106        properties.put(propName, propValue);
107    }
108
109    protected String[] getStringArrayProperty(String propName) {
110        return (String[]) properties.get(propName);
111    }
112
113    protected void setStringArrayProperty(String propName, Serializable propValue) {
114        if (propValue == null) {
115            return;
116        }
117        if (!(propValue instanceof String[])) {
118            log.error(String.format("Property %s needs a string array, invalid value %s", propName, propValue));
119        }
120        properties.put(propName, propValue);
121    }
122
123}