/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. package com.google.ie.web.controller;
  16. import com.google.appengine.api.datastore.Key;
  17. import com.google.appengine.api.datastore.KeyFactory;
  18. import com.google.apphosting.api.DeadlineExceededException;
  19. import com.google.ie.business.domain.EntityIndex;
  20. import com.google.ie.business.service.EntityIndexService;
  21. import com.google.ie.common.constants.IdeaExchangeConstants;
  22. import com.google.ie.common.util.EntityMapperUtility;
  23. import com.google.ie.common.util.SearchUtility;
  24. import org.apache.log4j.Logger;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Controller;
  27. import org.springframework.web.bind.annotation.PathVariable;
  28. import org.springframework.web.bind.annotation.RequestMapping;
  29. import java.io.Serializable;
  30. import java.util.GregorianCalendar;
  31. import javax.servlet.http.HttpServletRequest;
  32. /**
  33. * A controller that handles request for indexing un-indexed entities.
  34. *
  35. * @author Ashish K. Dahiya
  36. *
  37. */
  38. @Controller
  39. @RequestMapping("/indexer")
  40. public class IndexController {
  41. private static final Logger LOG = Logger.getLogger(IndexController.class);
  42. @Autowired
  43. private EntityIndexService entityIndexService;
  44. /**
  45. * Index the {@link EntityIndex} entity represented by the key
  46. *
  47. * @param request {@link HttpServletRequest} object
  48. * @param encodedKey the key of the entity to be indexed
  49. * @return View Name
  50. */
  51. @RequestMapping("/indexentity/{key}")
  52. public String indexEntity(HttpServletRequest request, @PathVariable String key) {
  53. LOG.debug("IndexController: Index Entity");
  54. String count = request.getHeader(IdeaExchangeConstants.APPENGINE_TASKRETRYCOUNT);
  55. if (count == null || count.trim().equals("0")) {
  56. long startTime = GregorianCalendar.getInstance().getTimeInMillis();
  57. try {
  58. Key indexKey = KeyFactory.stringToKey(key);
  59. /* retrieve and un-indexed entity */
  60. EntityIndex entityIndex = entityIndexService.getEntity(indexKey);
  61. if (entityIndex != null && entityIndex.getIndexed() == 0) {
  62. boolean flag = indexEntity(entityIndex);
  63. if (flag && LOG.isDebugEnabled()) {
  64. LOG.debug("Record for "
  65. + entityIndex.getKey().getKind()
  66. + " index in "
  67. + (GregorianCalendar.getInstance()
  68. .getTimeInMillis() - startTime)
  69. + " ms");
  70. }
  71. }
  72. } catch (DeadlineExceededException e) {
  73. LOG.error("DeadlineExceededException in indexIdeas", e);
  74. }
  75. } else {
  76. LOG.debug("Bypassing the entity index as the count is " + count);
  77. }
  78. return "queue/queue";
  79. }
  80. /**
  81. * Index an un-indexed entity and mark it as indexed.
  82. *
  83. */
  84. @RequestMapping("/index")
  85. public String indexEntity() {
  86. LOG.debug("IndexController: Indexing Entities");
  87. try {
  88. /* retrieve and un-indexed entity */
  89. EntityIndex entityIndex = entityIndexService.getUnIndexedEntity();
  90. boolean flag = indexEntity(entityIndex);
  91. if (flag) {
  92. LOG.info("indexing of entity with key :" + entityIndex.getParentKey()
  93. + " successful");
  94. }
  95. } catch (DeadlineExceededException e) {
  96. LOG.error("DeadlineExceededException in indexIdeas", e);
  97. }
  98. return "queue/queue";
  99. }
  100. /**
  101. * Index the {@link EntityIndex} object received as argument
  102. *
  103. * @param entityIndex {@link EntityIndex} object to be indexed
  104. * @return boolean whether he entity was indexed or not
  105. */
  106. private boolean indexEntity(final EntityIndex entityIndex) {
  107. boolean flag = false;
  108. try {
  109. Serializable entity = null;
  110. if (entityIndex != null) {
  111. String key = KeyFactory.keyToString(entityIndex
  112. .getParentKey());
  113. /* fetch entity details */
  114. entity = entityIndexService.getEntity(key, EntityMapperUtility
  115. .getEntity(entityIndex
  116. .getParentKey().getKind()));
  117. }
  118. if (entity != null) {
  119. LOG.debug("indexing entity with key :" + entityIndex.getParentKey());
  120. /* index the idea using compass */
  121. boolean entityIndexed = SearchUtility.indexEntity(entity);
  122. if (entityIndexed) {
  123. entityIndex.setIndexed(1);
  124. /* update the entity status from un-indexed to indexed */
  125. entityIndexService.updateEntityIndex(entityIndex);
  126. LOG.info("indexing of entity with key :" + entityIndex.getParentKey()
  127. + " successful");
  128. flag = true;
  129. }
  130. }
  131. } catch (DeadlineExceededException e) {
  132. LOG.error("DeadlineExceededException in indexIdeas", e);
  133. }
  134. return flag;
  135. }
  136. public EntityIndexService getEntityIndexService() {
  137. return entityIndexService;
  138. }
  139. public void setEntityIndexService(EntityIndexService entityIndexService) {
  140. this.entityIndexService = entityIndexService;
  141. }
  142. }