next.celebs.model.PhotoDao.java Source code

Java tutorial

Introduction

Here is the source code for next.celebs.model.PhotoDao.java

Source

/*
 * Copyright 2011 Vancouver Ywebb Consulting Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package next.celebs.model;

import java.util.HashMap;

import next.i.XLog;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.search.client.ImageResult;
import com.google.inject.Inject;

public class PhotoDao {

    // private Storage storage;
    private API api;
    // private Context ctx;

    @Inject
    public PhotoDao(API api) {
        //      init(api);
        this.api = api;
    }

    HashMap<Key, PhotoMap> searchedMaps = new HashMap<Key, PhotoMap>();

    private PhotoMap getPhotoMap(Key searchKey) {
        PhotoMap pm = searchedMaps.get(searchKey);
        if (pm == null) {
            pm = new PhotoMap(searchKey);
            searchedMaps.put(searchKey, pm);
        }
        return pm;
    }

    public void getPhotos(final Key searchKey, int numberPages, final API.Response<PhotoMap> response) {

        // if (storage.hasKey(searchKey)) {
        // String json = storage.getItem(searchKey);
        // Log.info("PhotoDao::getPhotos from Storage '"+ json + "'");
        // JsArray<Photo.JSO_> jsoArr = JsonUtils.safeEval(json);
        // int len = jsoArr.length();
        // PhotoMap pm = getPhotoMap(searchKey);
        // for (int i = 0; i < len; i++) {
        // Photo p = jsoArr.get(i);
        // pm.put(p.getUrl(), p);
        // }
        // response.read(pm);
        //
        // } else {
        //      XLog.info("PhotoDao::getPhotos from API" + searchKey);
        api.getPhotos(searchKey, numberPages, new API.Response<JsArray<ImageResult>>() {
            @Override
            public void read(JsArray<ImageResult> data) {
                int len = data.length();

                PhotoMap pm = getPhotoMap(searchKey);
                PhotoMap currentMap = new PhotoMap(searchKey);
                for (int i = 0; i < len; i++) {
                    ImageResult ir = data.get(i);

                    //               XLog.info(ir.getWidth() + "-" + ir.getThumbnailWidth() + "-" + ir.getTitleNoFormatting());

                    Photo p = new Photo.Bean(ir.getUrl(), ir.getThumbnailUrl(), ir.getWidth(), ir.getHeight(),
                            ir.getThumbnailWidth(), ir.getThumbnailHeight(), ir.getOriginalContextUrl());
                    pm.put(p.getUrl(), p);
                    currentMap.put(p.getUrl(), p);
                }
                // pm.save();
                response.read(currentMap);
            }

            @Override
            public void afterRead(String jsonData) {
                // storage.setItem(Key.Countries, jsonData);
                // Log.error("AFTER storing countries " + storage.getItem(Key.Country));
            }
        });
        // }
    }

    public void clear() {
        XLog.info("PhotoDao::clear");
        searchedMaps.clear();
        // storage.clear();
    }

    private final native String toJSON(JavaScriptObject jso) /*-{
                                                             return jso.toString();
                                                             }-*/;

    //   public void init(API api) {
    //      // this.storage = ctx.getStorage();
    //      this.api = api;
    //      // this.ctx = ctx;
    //   }

}