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        super(typeLabelOf(doc.getProperty(PROP_TYPE).getValue(String.class)));
044        this.doc = doc;
045    }
046
047    public DocumentModel getDocumentModel() {
048        return doc;
049    }
050
051    @Override
052    @SuppressWarnings("unchecked")
053    public List<String> getApplicableVersion() {
054        try {
055            return (List<String>) doc.getPropertyValue(PROP_APPLICABLE_VERSIONS);
056        } catch (PropertyException e) {
057            log.error("Error while reading applicable version", e);
058            return new ArrayList<>();
059        }
060    }
061
062    @Override
063    public String getContent() {
064        String encoding = "unset";
065        try {
066            Blob blob = (Blob) doc.getPropertyValue("file:content");
067            if (blob == null) {
068                return "";
069            }
070            encoding = blob.getEncoding();
071            if (encoding == null || encoding.equals("")) {
072                blob.setEncoding("utf-8");
073            }
074            return blob.getString();
075        } catch (IOException | PropertyException e) {
076            log.error("Error while reading content with encoding " + encoding, e);
077            return "ERROR : " + e.getMessage();
078        }
079    }
080
081    @Override
082    public String getRenderingType() {
083        try {
084            return (String) doc.getPropertyValue(PROP_RENDERING_TYPE);
085        } catch (PropertyException e) {
086            log.error("Error while reading rendering type", e);
087            return "";
088        }
089    }
090
091    @Override
092    public String getTarget() {
093        try {
094            return (String) doc.getPropertyValue(PROP_TARGET);
095        } catch (PropertyException e) {
096            log.error("Error while reading target", e);
097            return "";
098        }
099    }
100
101    @Override
102    public String getTargetType() {
103        try {
104            return (String) doc.getPropertyValue(PROP_TARGET_TYPE);
105        } catch (PropertyException e) {
106            log.error("Error while reading targetType", e);
107            return "";
108        }
109    }
110
111    @Override
112    public String getType() {
113        try {
114            return (String) doc.getPropertyValue(PROP_TYPE);
115        } catch (PropertyException e) {
116            log.error("Error while reading type", e);
117            return "";
118        }
119    }
120
121    @Override
122    public boolean isApproved() {
123        try {
124            Boolean approved = (Boolean) doc.getPropertyValue(PROP_NUXEO_APPROVED);
125            return approved == null ? false : approved.booleanValue();
126        } catch (PropertyException e) {
127            log.error("Error while reading type", e);
128            return false;
129        }
130    }
131
132    @Override
133    public String getId() {
134        try {
135            return (String) doc.getPropertyValue(PROP_DOCUMENTATION_ID);
136        } catch (PropertyException e) {
137            log.error("Error while reading target", e);
138            return "";
139        }
140    }
141
142    @Override
143    public String getUUID() {
144        return doc.getId();
145    }
146
147    @Override
148    public String getTitle() {
149        try {
150            return (String) doc.getPropertyValue("dc:title");
151        } catch (PropertyException e) {
152            log.error("Error while reading title", e);
153            return "";
154        }
155    }
156
157    @Override
158    @SuppressWarnings("unchecked")
159    public Map<String, String> getAttachments() {
160        Map<String, String> attachments = new LinkedMap();
161        try {
162            List<Map<String, Serializable>> atts = (List<Map<String, Serializable>>) doc.getPropertyValue("files:files");
163            if (atts != null) {
164                for (Map<String, Serializable> att : atts) {
165                    Blob attBlob = (Blob) att.get("file");
166                    if (attBlob.getEncoding() == null || attBlob.getEncoding().equals("")) {
167                        attBlob.setEncoding("utf-8");
168                    }
169                    attachments.put((String) att.get("filename"), attBlob.getString());
170                }
171            }
172        } catch (IOException | PropertyException e) {
173            log.error("Error while reading Attachments", e);
174        }
175        return attachments;
176    }
177
178    @Override
179    public boolean isPlaceHolder() {
180        return false;
181    }
182
183    @Override
184    public String getEditId() {
185        return getUUID();
186    }
187
188    @Override
189    public boolean isReadOnly() {
190        return false;
191    }
192}