/src/main/java/com/google/ie/business/dao/impl/CommentDaoImpl.java
Java | 100 lines | 59 code | 13 blank | 28 comment | 7 complexity | fb22fe7d9f22b56fa0731dd662d874a9 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.business.dao.impl; 17 18import com.google.ie.business.dao.CommentDao; 19import com.google.ie.business.domain.Comment; 20import com.google.ie.business.domain.IdeaComment; 21import com.google.ie.business.domain.ProjectComment; 22import com.google.ie.dto.RetrievalInfo; 23 24import org.springframework.transaction.annotation.Propagation; 25import org.springframework.transaction.annotation.Transactional; 26 27import java.util.ArrayList; 28import java.util.Collection; 29import java.util.HashMap; 30import java.util.HashSet; 31import java.util.List; 32import java.util.Set; 33 34import javax.jdo.Query; 35 36/** 37 * A JDO implementation object for CommentDao. 38 * 39 * @author Sachneet 40 * 41 */ 42public class CommentDaoImpl extends BaseDaoImpl implements CommentDao { 43 44 private static final String IDEA_KEY = "ideaKey"; 45 46 public CommentDaoImpl() { 47 48 } 49 50 @Override 51 @Transactional(readOnly = false, propagation = Propagation.REQUIRED) 52 public Comment saveComment(Comment comment) { 53 return getJdoTemplate().makePersistent(comment); 54 } 55 56 @SuppressWarnings("unchecked") 57 @Override 58 public List<Comment> getComments(String key, RetrievalInfo retrievalInfo, 59 String keyType) { 60 Query query = null; 61 try { 62 /* Create the query instance corresponding to the keyType */ 63 if (keyType.equals(IDEA_KEY)) { 64 query = getJdoTemplate().getPersistenceManagerFactory().getPersistenceManager() 65 .newQuery(IdeaComment.class); 66 } else { 67 query = getJdoTemplate().getPersistenceManagerFactory().getPersistenceManager() 68 .newQuery(ProjectComment.class); 69 } 70 /* Set of status to be matched */ 71 Set<String> setOfStatus = new HashSet<String>(); 72 setOfStatus.add(IdeaComment.STATUS_SAVED); 73 setOfStatus.add(IdeaComment.STATUS_FLAGGED); 74 query.setFilter(keyType + " == '" + key + "' && status == :setOfStatus"); 75 query.setOrdering(retrievalInfo.getOrderBy() + " " + retrievalInfo.getOrderType()); 76 /* 77 * Add the start index to the number of records required since 78 * internally the second argument is treated as the index up to 79 * which 80 * the entities are to be fetched 81 */ 82 query.setRange(retrievalInfo.getStartIndex(), retrievalInfo.getStartIndex() 83 + retrievalInfo.getNoOfRecords()); 84 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 85 hashMap.put("setOfStatus", setOfStatus); 86 Collection<Comment> collection = getJdoTemplate().find(query.toString(), hashMap); 87 if (collection != null && collection.size() > DaoConstants.ZERO) { 88 List<Comment> commentList = (new ArrayList<Comment>(collection)); 89 return commentList; 90 } 91 } finally { 92 if (query != null) { 93 query.closeAll(); 94 } 95 } 96 return null; 97 } 98 99} 100