next.celebs.dao.PhotoDao.java Source code

Java tutorial

Introduction

Here is the source code for next.celebs.dao.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.dao;

import java.util.HashMap;

import next.celebs.Log;
import next.celebs.api.API;
import next.celebs.api.API.Response;
import next.celebs.di.Context;
import next.celebs.di.Storage;
import next.celebs.model.Key;
import next.celebs.model.Photo;
import next.celebs.model.PhotoMap;

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

public class PhotoDao {

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

    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, storage);
            searchedMaps.put(searchKey, pm);
        }
        return pm;
    }

    public void getPhotos(final Key searchKey, int numberPages, final 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 {
        Log.info("PhotoDao::getPhotos from API");
        api.getPhotos(searchKey, numberPages, new Response<JsArray<ImageResult>>() {
            @Override
            public void read(JsArray<ImageResult> data) {
                int len = data.length();

                PhotoMap pm = getPhotoMap(searchKey);
                PhotoMap currentMap = new PhotoMap(searchKey, storage);
                for (int i = 0; i < len; i++) {
                    ImageResult ir = data.get(i);
                    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() {
        Log.info("PhotoDao::clear");
        searchedMaps.clear();
        storage.clear();
    }

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

    public void init(Context ctx) {
        this.storage = ctx.getStorage();
        this.api = ctx.getApi();
        this.ctx = ctx;
    }

}