Package org.rssowl.core.internal.persist.dao

Source Code of org.rssowl.core.internal.persist.dao.EntitiesToBeIndexedDAOImpl

/*   **********************************************************************  **
**   Copyright notice                                                       **
**                                                                          **
**   (c) 2005-2009 RSSOwl Development Team                                  **
**   http://www.rssowl.org/                                                 **
**                                                                          **
**   All rights reserved                                                    **
**                                                                          **
**   This program and the accompanying materials are made available under   **
**   the terms of the Eclipse Public License v1.0 which accompanies this    **
**   distribution, and is available at:                                     **
**   http://www.rssowl.org/legal/epl-v10.html                               **
**                                                                          **
**   A copy is found in the file epl-v10.html and important notices to the  **
**   license from the team is found in the textfile LICENSE.txt distributed **
**   in this package.                                                       **
**                                                                          **
**   This copyright notice MUST APPEAR in all copies of the file!           **
**                                                                          **
**   Contributors:                                                          **
**     RSSOwl Development Team - initial API and implementation             **
**                                                                          **
**  **********************************************************************  */
package org.rssowl.core.internal.persist.dao;

import org.eclipse.core.runtime.Assert;
import org.rssowl.core.internal.persist.service.DatabaseEvent;
import org.rssowl.core.internal.persist.service.EntityIdsByEventType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class EntitiesToBeIndexedDAOImpl extends AbstractPersistableDAO<EntityIdsByEventType>  {

  private volatile EntityIdsByEventType fEntityIds;
  public EntitiesToBeIndexedDAOImpl() {
    super(EntityIdsByEventType.class, true);
  }

  public final EntityIdsByEventType load() {
    return fEntityIds;
  }

  @Override
  protected void onDatabaseOpened(DatabaseEvent event) {
    super.onDatabaseOpened(event);
    EntityIdsByEventType entityIds = doLoad();
    if (entityIds == null) {
      entityIds = new EntityIdsByEventType(false);
      save(entityIds);
    }
    fEntityIds = entityIds;
  }

  @Override
  protected void preCommit() {
    //Do nothing
  }

  private EntityIdsByEventType doLoad() {
    Collection<EntityIdsByEventType> entityIdsCollection = super.loadAll();
    Assert.isLegal(entityIdsCollection.size() <= 1, "There shouldn't be more than 1 EntityIdsByEventType, size: " + entityIdsCollection.size()); //$NON-NLS-1$

    for (EntityIdsByEventType entityIds : entityIdsCollection) {
      /* Return the first one since we assert that we don't have more than one */
      return entityIds;
    }

    return null;
  }

  @Override
  protected void onDatabaseClosed(DatabaseEvent event) {
    super.onDatabaseClosed(event);
    fEntityIds = null;
  }

  @Override
  public final void delete(EntityIdsByEventType newsCounter) {
    if (!newsCounter.equals(load()))
      throw new IllegalArgumentException("Only a single entity should be used. " + "Trying to delete a non-existent one."); //$NON-NLS-1$ //$NON-NLS-2$

    super.delete(newsCounter);
  }

  @Override
  public Collection<EntityIdsByEventType> loadAll() {
    List<EntityIdsByEventType> newsCounters = new ArrayList<EntityIdsByEventType>(1);
    newsCounters.add(load());
    return newsCounters;
  }

  @Override
  public final void saveAll(Collection<EntityIdsByEventType> entities) {
    if (entities.size() > 1) {
      throw new IllegalArgumentException("Only a single entity can be stored"); //$NON-NLS-1$
    }
    super.saveAll(entities);
  }

  @Override
  protected final void doSave(EntityIdsByEventType entity) {
    if (!fDb.ext().isStored(entity) && (load() != null))
      throw new IllegalArgumentException("Only a single entity can be stored"); //$NON-NLS-1$

    super.doSave(entity);
  }
}
TOP

Related Classes of org.rssowl.core.internal.persist.dao.EntitiesToBeIndexedDAOImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.