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