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