001/*
002 * (C) Copyright 2012 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.util.Arrays;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.apidoc.api.AbstractDocumentationItem;
027import org.nuxeo.apidoc.api.DocumentationItem;
028import org.nuxeo.apidoc.api.NuxeoArtifact;
029
030import com.cforcoding.jmd.MarkDownParserAndSanitizer;
031import com.fasterxml.jackson.annotation.JsonCreator;
032import com.fasterxml.jackson.annotation.JsonIgnore;
033import com.fasterxml.jackson.annotation.JsonProperty;
034
035public class ResourceDocumentationItem extends AbstractDocumentationItem implements DocumentationItem {
036
037    protected final String content;
038
039    protected final String filename;
040
041    protected final String target;
042
043    protected final String targetType;
044
045    protected final String type;
046
047    protected final List<String> applicableVersion;
048
049    @JsonCreator
050    public ResourceDocumentationItem(@JsonProperty("filename") String filename, @JsonProperty("content") String content,
051            @JsonProperty("type") String type, @JsonProperty("target") String target, @JsonProperty("targetType") String targetType,
052            @JsonProperty("applicableVersion") List<String> applicableVersion, @JsonProperty("typeLabel") String typeLabel) {
053        super(typeLabel);
054        this.content = content;
055        this.filename = filename;
056        this.type = type;
057        this.target = target;
058        this.targetType = targetType;
059        this.applicableVersion = applicableVersion;
060    }
061
062
063    public ResourceDocumentationItem(String filename, String content, String type, NuxeoArtifact target) {
064        this(filename, content, type, target.getId(), target.getArtifactType(), Arrays.asList(target.getVersion()), typeLabelOf(type));
065    }
066
067    public ResourceDocumentationItem(ResourceDocumentationItem other, NuxeoArtifact target) {
068        this(other.filename, other.content, other.type, target);
069    }
070
071    @Override
072    @JsonIgnore
073    public String getTitle() {
074        return getCleanName() + " " + target;
075    }
076
077    protected String getCleanName() {
078        if (filename == null || filename.toLowerCase().startsWith("readme")) {
079            return "ReadMe";
080        }
081        int idx = filename.indexOf(".");
082        if (idx > 0) {
083            return filename.substring(0, idx);
084        }
085        return filename;
086    }
087
088    @Override
089    @JsonIgnore
090    public String getContent() {
091        MarkDownParserAndSanitizer parser = new MarkDownParserAndSanitizer();
092        String xHtml = parser.transform(content);
093        return xHtml;
094    }
095
096    @Override
097    @JsonIgnore
098    public String getType() {
099        return type;
100    }
101
102    @Override
103    @JsonIgnore
104    public String getRenderingType() {
105        return "html";
106    }
107
108    @Override
109    public List<String> getApplicableVersion() {
110        return applicableVersion;
111    }
112
113    @Override
114    public String getTarget() {
115        return target;
116    }
117
118    @Override
119    public String getTargetType() {
120        return targetType;
121    }
122
123    @Override
124    @JsonIgnore
125    public boolean isApproved() {
126        return true;
127    }
128
129    @Override
130    @JsonIgnore
131    public String getId() {
132        return getTargetType() + "--" + filename;
133    }
134
135    @Override
136    @JsonIgnore
137    public String getUUID() {
138        return null;
139    }
140
141    @Override
142    @JsonIgnore
143    public Map<String, String> getAttachments() {
144        return new HashMap<>();
145    }
146
147    @Override
148    @JsonIgnore
149    public boolean isPlaceHolder() {
150        return true;
151    }
152
153    @Override
154    @JsonIgnore
155    public String getEditId() {
156        return null;
157    }
158
159    @Override
160    @JsonIgnore
161    public boolean isReadOnly() {
162        return true;
163    }
164}