PageRenderTime 27ms CodeModel.GetById 21ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/google/ie/business/dao/impl/EntityIndexDaoImpl.java

http://thoughtsite.googlecode.com/
Java | 100 lines | 60 code | 15 blank | 25 comment | 8 complexity | d83ad32565f547e1263d0c9f5a23a163 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.appengine.api.datastore.Key;
 19import com.google.ie.business.dao.EntityIndexDao;
 20import com.google.ie.business.domain.EntityIndex;
 21
 22import java.util.ArrayList;
 23import java.util.Collection;
 24import java.util.List;
 25
 26import javax.jdo.PersistenceManager;
 27import javax.jdo.Query;
 28
 29/**
 30 * A JDO implementation object for {@link EntityIndexDao}.
 31 * 
 32 * @author Ashish K. Dahiya
 33 */
 34public class EntityIndexDaoImpl extends BaseDaoImpl implements EntityIndexDao {
 35
 36    public EntityIndexDaoImpl() {
 37    }
 38
 39    @Override
 40    public EntityIndex updateEntityIndex(EntityIndex entityIndex) {
 41        if (entityIndex != null) {
 42
 43            /*
 44             * Parent key is reset to null as either id or parent key can be
 45             * set for an entity
 46             */
 47            entityIndex.setParentKey(null);
 48            return getJdoTemplate().makePersistent(entityIndex);
 49        }
 50        return null;
 51    }
 52
 53    @SuppressWarnings("unchecked")
 54    @Override
 55    public EntityIndex getUnIndexedEntity() {
 56        Query query = null;
 57        try {
 58
 59            PersistenceManager pm = getJdoTemplate().getPersistenceManagerFactory()
 60                            .getPersistenceManager();
 61            query = pm.newQuery(EntityIndex.class);
 62            query.setFilter("indexed == 0");
 63            // query.setRange(0, 1);
 64            List<EntityIndex> results = (List<EntityIndex>) query.execute();
 65
 66            if (results != null && results.size() > 0) {
 67                EntityIndex entityIndex = results.get(DaoConstants.ZERO);
 68                entityIndex = pm.detachCopy(entityIndex);
 69                return entityIndex;
 70            }
 71        } finally {
 72            if (query != null)
 73                query.closeAll();
 74        }
 75        return null;
 76    }
 77
 78    @Override
 79    public EntityIndex createEntityIndex(String parentKey) {
 80
 81        EntityIndex entityIndex = new EntityIndex(parentKey);
 82        /* Set the indexd flag to UnIndexed */
 83        entityIndex.setIndexed(DaoConstants.UNINDEXED);
 84        return getJdoTemplate().makePersistent(entityIndex);
 85    }
 86
 87    @Override
 88    public EntityIndex findEntityByPrimaryKey(Key key) {
 89        Collection<EntityIndex> results = getJdoTemplate().find(EntityIndex.class,
 90                        "key== :keyParam", null, key);
 91        results = getJdoTemplate().detachCopyAll(results);
 92        List<EntityIndex> list = new ArrayList<EntityIndex>(results);
 93        if (list.size() > DaoConstants.ZERO) {
 94            EntityIndex entity = list.get(DaoConstants.ZERO);
 95            return entity;
 96        }
 97        return null;
 98    }
 99}
100