001/*
002 * (C) Copyright 2006-2008 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 *     troger
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.pictures.tiles.gwt.client.model;
023
024import com.allen_sauer.gwt.log.client.Log;
025import com.google.gwt.http.client.Request;
026import com.google.gwt.http.client.RequestBuilder;
027import com.google.gwt.http.client.RequestCallback;
028import com.google.gwt.http.client.RequestException;
029import com.google.gwt.http.client.Response;
030import com.google.gwt.json.client.JSONNumber;
031import com.google.gwt.json.client.JSONObject;
032import com.google.gwt.json.client.JSONParser;
033import com.google.gwt.json.client.JSONString;
034import com.google.gwt.user.client.Window;
035
036/**
037 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
038 */
039public class TilingInfo {
040
041    private final String repoId;
042
043    private final String docId;
044
045    private final String contextPath;
046
047    private boolean initialized;
048
049    private int originalImageWidth;
050
051    private int originalImageHeight;
052
053    private double zoom;
054
055    private int tileWidth;
056
057    private int tileHeight;
058
059    private int nbXTiles;
060
061    private int nbYTiles;
062
063    private int maxTiles;
064
065    private long lastModificationDate;
066
067    public TilingInfo(String repoId, String docId, String contextPath) {
068        this.repoId = repoId;
069        this.docId = docId;
070        this.contextPath = contextPath;
071    }
072
073    public TilingInfo(String repoId, String docId, String contextPath, int tileWidth, int tileHeight, int maxTiles) {
074        this(repoId, docId, contextPath);
075        this.tileWidth = tileWidth;
076        this.tileHeight = tileHeight;
077        this.maxTiles = maxTiles;
078    }
079
080    public TilingInfo(TilingInfo source) {
081        docId = source.docId;
082        repoId = source.repoId;
083        contextPath = source.contextPath;
084        originalImageWidth = source.originalImageWidth;
085        originalImageHeight = source.originalImageHeight;
086        zoom = source.zoom;
087        tileWidth = source.tileWidth;
088        tileHeight = source.tileHeight;
089        nbXTiles = source.nbXTiles;
090        nbYTiles = source.nbYTiles;
091        maxTiles = source.maxTiles;
092        initialized = source.initialized;
093        lastModificationDate = source.lastModificationDate;
094    }
095
096    /**
097     * @return the repoId.
098     */
099    public String getRepoId() {
100        return repoId;
101    }
102
103    /**
104     * @return the docId.
105     */
106    public String getDocId() {
107        return docId;
108    }
109
110    /**
111     * @return the initialized.
112     */
113    public boolean isInitialized() {
114        return initialized;
115    }
116
117    /**
118     * @return the originalImageWidth.
119     */
120    public int getOriginalImageWidth() {
121        return originalImageWidth;
122    }
123
124    /**
125     * @return the originalImageHeight.
126     */
127    public int getOriginalImageHeight() {
128        return originalImageHeight;
129    }
130
131    /**
132     * @return the zoom.
133     */
134    public double getZoom() {
135        return zoom;
136    }
137
138    /**
139     * @return the tileWidth.
140     */
141    public int getTileWidth() {
142        return tileWidth;
143    }
144
145    /**
146     * @return the tileHeight.
147     */
148    public int getTileHeight() {
149        return tileHeight;
150    }
151
152    public void setTileWidth(int tileWidth) {
153        this.tileWidth = tileWidth;
154    }
155
156    public void setTileHeight(int tileHeight) {
157        this.tileHeight = tileHeight;
158    }
159
160    /**
161     * @return the nbXTiles.
162     */
163    public int getNbXTiles() {
164        return nbXTiles;
165    }
166
167    /**
168     * @return the nbYTiles.
169     */
170    public int getNbYTiles() {
171        return nbYTiles;
172    }
173
174    /**
175     * @return the maxTiles.
176     */
177    public int getMaxTiles() {
178        return maxTiles;
179    }
180
181    /**
182     * Set the maxTiles.
183     */
184    public void setMaxTiles(int maxTiles) {
185        this.maxTiles = maxTiles;
186    }
187
188    public long getLastModificationDate() {
189        return lastModificationDate;
190    }
191
192    public void updateTilingInfo() {
193        updateTilingInfo(null);
194    }
195
196    public void updateTilingInfo(final TilingInfoCallback callback) {
197        RequestBuilder getRequest = new RequestBuilder(RequestBuilder.GET, getBaseUrl() + "?format=json");
198        try {
199            getRequest.sendRequest(null, new RequestCallback() {
200
201                public void onError(Request arg0, Throwable arg1) {
202                    Log.error("Error sending tiling info request: " + arg1);
203                }
204
205                public void onResponseReceived(Request arg0, Response resp) {
206                    parseResponse(resp.getText());
207                    if (callback != null) {
208                        callback.tilingInfoUpdated();
209                    }
210                }
211            });
212        } catch (RequestException e) {
213            Window.alert("Error getting the tiling server: " + e);
214        }
215    }
216
217    public void parseResponse(String response) {
218        if ("".equals(response)) {
219            return;
220        }
221        JSONObject jsonValue = (JSONObject) JSONParser.parse(response);
222        JSONObject tileInfo = (JSONObject) jsonValue.get("tileInfo");
223        JSONObject originalImage = (JSONObject) jsonValue.get("originalImage");
224        JSONNumber zoomFactor = (JSONNumber) tileInfo.get("zoom");
225        JSONNumber widthJS = (JSONNumber) originalImage.get("width");
226        JSONNumber heightJS = (JSONNumber) originalImage.get("height");
227        JSONNumber xTilesJS = (JSONNumber) tileInfo.get("xtiles");
228        JSONNumber yTilesJS = (JSONNumber) tileInfo.get("ytiles");
229        JSONNumber maxTilesJS = (JSONNumber) tileInfo.get("maxtiles");
230        JSONNumber tileWidthJS = (JSONNumber) tileInfo.get("tileWidth");
231        JSONNumber tileHeightJS = (JSONNumber) tileInfo.get("tileHeight");
232
233        zoom = zoomFactor.doubleValue();
234        originalImageWidth = new Double(widthJS.doubleValue()).intValue();
235        originalImageHeight = new Double(heightJS.doubleValue()).intValue();
236        nbXTiles = new Double(xTilesJS.doubleValue()).intValue();
237        nbYTiles = new Double(yTilesJS.doubleValue()).intValue();
238        maxTiles = new Double(maxTilesJS.doubleValue()).intValue();
239        tileWidth = new Double(tileWidthJS.doubleValue()).intValue();
240        tileHeight = new Double(tileHeightJS.doubleValue()).intValue();
241
242        JSONObject additionalInfo = (JSONObject) jsonValue.get("additionalInfo");
243        JSONString lastModificationDateJS = (JSONString) additionalInfo.get("lastModificationDate");
244        lastModificationDate = Long.parseLong(lastModificationDateJS.stringValue());
245
246        initialized = true;
247    }
248
249    public String getBaseUrl() {
250        return contextPath + "/restAPI/getTiles/" + this.getRepoId() + "/" + this.getDocId() + "/"
251                + this.getTileWidth() + "/" + this.getTileHeight() + "/" + this.getMaxTiles();
252    }
253
254}