Subversion Repositories SmartDukaan

Rev

Rev 20424 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.hotspotstore.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hotspotstore.model.Banner;
import com.hotspotstore.model.EntityShort;
import com.hotspotstore.model.Snippet;
import com.hotspotstore.storage.Mongo;

public class HomeController extends BaseController {

        private static final long serialVersionUID = 1L;
        private List<Banner> activeBanners;
        private Map<String, List<EntityShort>> snippetMap = new HashMap<String, List<EntityShort>>();

        public Map<String, List<EntityShort>> getSnippetMap() {
                return snippetMap;
        }

        public void setSnippetMap(Map<String, List<EntityShort>> snippetMap) {
                this.snippetMap = snippetMap;
        }

        public List<Banner> getActiveBanners() {
                return activeBanners;
        }

        public void setActiveBanners(List<Banner> activeBanners) {
                this.activeBanners = activeBanners;
        }

        @Action("/")
        public String index() throws JSONException{
                activeBanners = Mongo.getAllActiveBanners();
                List<Snippet> snippets = Mongo.getSnippets();
                for (Snippet s : snippets){
                        ArrayList<EntityShort> entityShortList = new ArrayList<EntityShort>();
                        for (Long entityId: s.getEntities()){
                                JSONObject entity;
                                try {
                                        entity = Mongo.getEntityById(entityId);
                                } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                        continue;
                                }
                                EntityShort entityShort = new EntityShort();
                                entityShort.setEntityId(entityId);
                                entityShort.setTitle(entity.getString("title"));
                                entityShort.setThumbnail(entity.getString("defaultImageUrl"));
                                entityShort.setUrl(entity.getString("url"));
                                JSONArray keySpecs = entity.getJSONArray("keySpecs");
                                ArrayList<String> specs = new ArrayList<String>();
                                for (int i = 0; i < keySpecs.length(); i++) {
                                        specs.add(keySpecs.getString(i));
                                }
                                entityShort.setKeySpecs(specs);
                                entityShortList.add(entityShort);
                        }
                        snippetMap.put(s.getSnippet(), entityShortList);
                }
                return "index";
        }
}