PageRenderTime 366ms CodeModel.GetById 191ms app.highlight 10ms RepoModel.GetById 162ms app.codeStats 0ms

/src/main/java/com/google/ie/web/controller/IndexController.java

http://thoughtsite.googlecode.com/
Java | 161 lines | 100 code | 19 blank | 42 comment | 16 complexity | 19eb6eee8e05f0f7c206750ed18f8d71 MD5 | raw file
  1/* Copyright 2010 Google Inc.
  2 * 
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 * 
  7 *      http://www.apache.org/licenses/LICENSE-2.0
  8 * 
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS.
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License
 14 */
 15
 16package com.google.ie.web.controller;
 17
 18import com.google.appengine.api.datastore.Key;
 19import com.google.appengine.api.datastore.KeyFactory;
 20import com.google.apphosting.api.DeadlineExceededException;
 21import com.google.ie.business.domain.EntityIndex;
 22import com.google.ie.business.service.EntityIndexService;
 23import com.google.ie.common.constants.IdeaExchangeConstants;
 24import com.google.ie.common.util.EntityMapperUtility;
 25import com.google.ie.common.util.SearchUtility;
 26
 27import org.apache.log4j.Logger;
 28import org.springframework.beans.factory.annotation.Autowired;
 29import org.springframework.stereotype.Controller;
 30import org.springframework.web.bind.annotation.PathVariable;
 31import org.springframework.web.bind.annotation.RequestMapping;
 32
 33import java.io.Serializable;
 34import java.util.GregorianCalendar;
 35
 36import javax.servlet.http.HttpServletRequest;
 37
 38/**
 39 * A controller that handles request for indexing un-indexed entities.
 40 * 
 41 * @author Ashish K. Dahiya
 42 * 
 43 */
 44
 45@Controller
 46@RequestMapping("/indexer")
 47public class IndexController {
 48    private static final Logger LOG = Logger.getLogger(IndexController.class);
 49    @Autowired
 50    private EntityIndexService entityIndexService;
 51
 52    /**
 53     * Index the {@link EntityIndex} entity represented by the key
 54     * 
 55     * @param request {@link HttpServletRequest} object
 56     * @param encodedKey the key of the entity to be indexed
 57     * @return View Name
 58     */
 59    @RequestMapping("/indexentity/{key}")
 60    public String indexEntity(HttpServletRequest request, @PathVariable String key) {
 61        LOG.debug("IndexController: Index Entity");
 62        String count = request.getHeader(IdeaExchangeConstants.APPENGINE_TASKRETRYCOUNT);
 63
 64        if (count == null || count.trim().equals("0")) {
 65            long startTime = GregorianCalendar.getInstance().getTimeInMillis();
 66            try {
 67                Key indexKey = KeyFactory.stringToKey(key);
 68                /* retrieve and un-indexed entity */
 69                EntityIndex entityIndex = entityIndexService.getEntity(indexKey);
 70                if (entityIndex != null && entityIndex.getIndexed() == 0) {
 71                    boolean flag = indexEntity(entityIndex);
 72                    if (flag && LOG.isDebugEnabled()) {
 73                        LOG.debug("Record for "
 74                                        + entityIndex.getKey().getKind()
 75                                        + " index in "
 76                                        + (GregorianCalendar.getInstance()
 77                                        .getTimeInMillis() - startTime)
 78                                        + " ms");
 79                    }
 80                }
 81
 82            } catch (DeadlineExceededException e) {
 83                LOG.error("DeadlineExceededException in indexIdeas", e);
 84            }
 85        } else {
 86            LOG.debug("Bypassing the entity index as the count is " + count);
 87        }
 88        return "queue/queue";
 89    }
 90
 91    /**
 92     * Index an un-indexed entity and mark it as indexed.
 93     * 
 94     */
 95    @RequestMapping("/index")
 96    public String indexEntity() {
 97        LOG.debug("IndexController: Indexing Entities");
 98        try {
 99            /* retrieve and un-indexed entity */
100            EntityIndex entityIndex = entityIndexService.getUnIndexedEntity();
101            boolean flag = indexEntity(entityIndex);
102            if (flag) {
103                LOG.info("indexing of entity with key :" + entityIndex.getParentKey()
104                                + "  successful");
105            }
106
107        } catch (DeadlineExceededException e) {
108            LOG.error("DeadlineExceededException in indexIdeas", e);
109        }
110        return "queue/queue";
111    }
112
113    /**
114     * Index the {@link EntityIndex} object received as argument
115     * 
116     * @param entityIndex {@link EntityIndex} object to be indexed
117     * @return boolean whether he entity was indexed or not
118     */
119    private boolean indexEntity(final EntityIndex entityIndex) {
120        boolean flag = false;
121        try {
122
123            Serializable entity = null;
124            if (entityIndex != null) {
125                String key = KeyFactory.keyToString(entityIndex
126                                .getParentKey());
127                /* fetch entity details */
128                entity = entityIndexService.getEntity(key, EntityMapperUtility
129                                .getEntity(entityIndex
130                                .getParentKey().getKind()));
131            }
132            if (entity != null) {
133                LOG.debug("indexing entity with key :" + entityIndex.getParentKey());
134                /* index the idea using compass */
135                boolean entityIndexed = SearchUtility.indexEntity(entity);
136                if (entityIndexed) {
137                    entityIndex.setIndexed(1);
138                    /* update the entity status from un-indexed to indexed */
139                    entityIndexService.updateEntityIndex(entityIndex);
140                    LOG.info("indexing of entity with key :" + entityIndex.getParentKey()
141                                    + "  successful");
142                    flag = true;
143                }
144            }
145
146        } catch (DeadlineExceededException e) {
147            LOG.error("DeadlineExceededException in indexIdeas", e);
148        }
149        return flag;
150    }
151
152    public EntityIndexService getEntityIndexService() {
153        return entityIndexService;
154    }
155
156    public void setEntityIndexService(EntityIndexService entityIndexService) {
157        this.entityIndexService = entityIndexService;
158    }
159
160}
161