001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (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 *     Thierry Delprat
016 */
017package org.nuxeo.apidoc.documentation;
018
019import java.io.IOException;
020import java.io.Serializable;
021import java.util.ArrayList;
022import java.util.List;
023import java.util.Map;
024
025import org.apache.commons.collections.map.LinkedMap;
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.apidoc.api.AbstractDocumentationItem;
029import org.nuxeo.apidoc.api.DocumentationItem;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.PropertyException;
033
034public class DocumentationItemDocAdapter extends AbstractDocumentationItem implements DocumentationItem {
035
036    protected static final Log log = LogFactory.getLog(DocumentationItemDocAdapter.class);
037
038    protected final DocumentModel doc;
039
040    public DocumentationItemDocAdapter(DocumentModel doc) {
041        this.doc = doc;
042    }
043
044    public DocumentModel getDocumentModel() {
045        return doc;
046    }
047
048    @Override
049    @SuppressWarnings("unchecked")
050    public List<String> getApplicableVersion() {
051        try {
052            return (List<String>) doc.getPropertyValue(PROP_APPLICABLE_VERSIONS);
053        } catch (PropertyException e) {
054            log.error("Error while reading applicable version", e);
055            return new ArrayList<String>();
056        }
057    }
058
059    @Override
060    public String getContent() {
061        String encoding = "unset";
062        try {
063            Blob blob = (Blob) doc.getPropertyValue("file:content");
064            if (blob == null) {
065                return "";
066            }
067            encoding = blob.getEncoding();
068            if (encoding == null || encoding.equals("")) {
069                blob.setEncoding("utf-8");
070            }
071            return blob.getString();
072        } catch (IOException | PropertyException e) {
073            log.error("Error while reading content with encoding " + encoding, e);
074            return "ERROR : " + e.getMessage();
075        }
076    }
077
078    @Override
079    public String getRenderingType() {
080        try {
081            return (String) doc.getPropertyValue(PROP_RENDERING_TYPE);
082        } catch (PropertyException e) {
083            log.error("Error while reading rendering type", e);
084            return "";
085        }
086    }
087
088    @Override
089    public String getTarget() {
090        try {
091            return (String) doc.getPropertyValue(PROP_TARGET);
092        } catch (PropertyException e) {
093            log.error("Error while reading target", e);
094            return "";
095        }
096    }
097
098    @Override
099    public String getTargetType() {
100        try {
101            return (String) doc.getPropertyValue(PROP_TARGET_TYPE);
102        } catch (PropertyException e) {
103            log.error("Error while reading targetType", e);
104            return "";
105        }
106    }
107
108    @Override
109    public String getType() {
110        try {
111            return (String) doc.getPropertyValue(PROP_TYPE);
112        } catch (PropertyException e) {
113            log.error("Error while reading type", e);
114            return "";
115        }
116    }
117
118    @Override
119    public boolean isApproved() {
120        try {
121            Boolean approved = (Boolean) doc.getPropertyValue(PROP_NUXEO_APPROVED);
122            return approved == null ? false : approved.booleanValue();
123        } catch (PropertyException e) {
124            log.error("Error while reading type", e);
125            return false;
126        }
127    }
128
129    @Override
130    public String getId() {
131        try {
132            return (String) doc.getPropertyValue(PROP_DOCUMENTATION_ID);
133        } catch (PropertyException e) {
134            log.error("Error while reading target", e);
135            return "";
136        }
137    }
138
139    @Override
140    public String getUUID() {
141        return doc.getId();
142    }
143
144    @Override
145    public String getTitle() {
146        try {
147            return (String) doc.getPropertyValue("dc:title");
148        } catch (PropertyException e) {
149            log.error("Error while reading title", e);
150            return "";
151        }
152    }
153
154    @Override
155    @SuppressWarnings("unchecked")
156    public Map<String, String> getAttachments() {
157        Map<String, String> attachments = new LinkedMap();
158        try {
159            List<Map<String, Serializable>> atts = (List<Map<String, Serializable>>) doc.getPropertyValue("files:files");
160            if (atts != null) {
161                for (Map<String, Serializable> att : atts) {
162                    Blob attBlob = (Blob) att.get("file");
163                    if (attBlob.getEncoding() == null || attBlob.getEncoding().equals("")) {
164                        attBlob.setEncoding("utf-8");
165                    }
166                    attachments.put((String) att.get("filename"), attBlob.getString());
167                }
168            }
169        } catch (IOException | PropertyException e) {
170            log.error("Error while reading Attachments", e);
171        }
172        return attachments;
173    }
174
175    @Override
176    public boolean isPlaceHolder() {
177        return false;
178    }
179
180    @Override
181    public String getEditId() {
182        return getUUID();
183    }
184
185    public boolean isReadOnly() {
186        return false;
187    }
188}