001/*
002 * (C) Copyright 2006-2016 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 *     Tiago Cardoso <tcardoso@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.threed.transmissionFormat;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.core.api.Blob;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.PropertyException;
026import org.nuxeo.ecm.platform.threed.ThreeDInfo;
027import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions;
028
029import java.io.Serializable;
030import java.util.Map;
031
032/**
033 * Small DTO to precompute transmission formats URLs for JSF
034 *
035 * @since 8.4
036 */
037public class TransmissionFormatItem {
038
039    public static final Log log = LogFactory.getLog(org.nuxeo.ecm.platform.threed.renderView.RenderViewItem.class);
040
041    protected final DocumentModel doc;
042
043    protected final int position;
044
045    protected final String blobPropertyName;
046
047    protected String filename;
048
049    protected Long percPoly;
050
051    protected Long maxPoly;
052
053    protected Long percTex;
054
055    protected String maxTex;
056
057    protected Long size;
058
059    protected ThreeDInfo info;
060
061    protected String name;
062
063    public TransmissionFormatItem(DocumentModel doc, String basePropertyPath, int position) {
064        this.doc = doc;
065        this.position = position;
066        String propertyPath = basePropertyPath + "/" + position;
067        blobPropertyName = propertyPath + "/content";
068        try {
069            Blob blob = (Blob) doc.getPropertyValue(blobPropertyName);
070            filename = blob.getFilename();
071            percPoly = (Long) doc.getPropertyValue(propertyPath + "/percPoly");
072            maxPoly = (Long) doc.getPropertyValue(propertyPath + "/maxPoly");
073            percTex = (Long) doc.getPropertyValue(propertyPath + "/percTex");
074            maxTex = (String) doc.getPropertyValue(propertyPath + "/maxTex");
075            name = (String) doc.getPropertyValue(propertyPath + "/name");
076            info = new ThreeDInfo((Map<String, Serializable>) doc.getPropertyValue(propertyPath + "/info"));
077            size = blob.getLength();
078        } catch (PropertyException e) {
079            log.warn(e);
080        }
081    }
082
083    private String formatAsReadable(long value, long base, String unit) {
084        if (value < base) {
085            return value + " " + unit;
086        }
087        int exp = (int) (Math.log(value) / Math.log(base));
088        String pre = String.valueOf(("kMGTPE").charAt(exp - 1));
089        return String.format("%.1f %s%s", value / Math.pow(base, exp), pre, unit);
090    }
091
092    public String getSrc() {
093        return DocumentModelFunctions.bigFileUrl(doc, blobPropertyName, filename);
094    }
095
096    public String getPercPoly() {
097        return (percPoly == null) ? null : percPoly.toString();
098    }
099
100    public String getMaxPoly() {
101        if (maxPoly == null) {
102            return null;
103        }
104        return formatAsReadable(maxPoly, 1000, "");
105    }
106
107    public String getPercTex() {
108        return (percTex == null) ? null : percTex.toString();
109    }
110
111    public String getMaxTex() {
112        return (maxTex == null) ? null : maxTex;
113    }
114
115    public String getSize() {
116        return size.toString();
117    }
118
119    public ThreeDInfo getInfo() {
120        return info;
121    }
122
123    public String getPolygons() {
124        Long polygons = info.getPolygons();
125        if (polygons == null) {
126            return "-";
127        }
128        return formatAsReadable(polygons, 1000, "");
129    }
130
131    public String getTextureSize() {
132        Long texturesSize = info.getTexturesSize();
133        if (texturesSize == null) {
134            return "-";
135        }
136        return formatAsReadable(texturesSize, 1024, "B");
137    }
138
139    public Boolean getHasTextures() {
140        return info.getTexturesSize() > 0;
141    }
142
143    public Boolean getGeometryLodSuccess() {
144        return info.getGeometryLodSuccess();
145    }
146
147    public Boolean getTextureLodSuccess() {
148        return info.getTextureLodSuccess();
149    }
150
151    public String getName() {
152        return name;
153    }
154
155}