/**
   * Caches the type in the entity cache if it is enabled.
   *
   * @param type the type
   */
  @Override
  public void cacheResult(Type type) {
    entityCache.putResult(
        TypeModelImpl.ENTITY_CACHE_ENABLED, TypeImpl.class, type.getPrimaryKey(), type);

    type.resetOriginalValues();
  }
  /**
   * Creates a new type with the primary key. Does not add the type to the database.
   *
   * @param typeId the primary key for the new type
   * @return the new type
   */
  @Override
  public Type create(long typeId) {
    Type type = new TypeImpl();

    type.setNew(true);
    type.setPrimaryKey(typeId);

    return type;
  }
  @Override
  public void clearCache(List<Type> types) {
    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

    for (Type type : types) {
      entityCache.removeResult(
          TypeModelImpl.ENTITY_CACHE_ENABLED, TypeImpl.class, type.getPrimaryKey());
    }
  }
 /**
  * Caches the types in the entity cache if it is enabled.
  *
  * @param types the types
  */
 @Override
 public void cacheResult(List<Type> types) {
   for (Type type : types) {
     if (entityCache.getResult(
             TypeModelImpl.ENTITY_CACHE_ENABLED, TypeImpl.class, type.getPrimaryKey())
         == null) {
       cacheResult(type);
     } else {
       type.resetOriginalValues();
     }
   }
 }
  protected Type toUnwrappedModel(Type type) {
    if (type instanceof TypeImpl) {
      return type;
    }

    TypeImpl typeImpl = new TypeImpl();

    typeImpl.setNew(type.isNew());
    typeImpl.setPrimaryKey(type.getPrimaryKey());

    typeImpl.setTypeId(type.getTypeId());
    typeImpl.setGroupId(type.getGroupId());
    typeImpl.setName(type.getName());

    return typeImpl;
  }
  @Override
  protected Type removeImpl(Type type) {
    type = toUnwrappedModel(type);

    Session session = null;

    try {
      session = openSession();

      if (!session.contains(type)) {
        type = (Type) session.get(TypeImpl.class, type.getPrimaryKeyObj());
      }

      if (type != null) {
        session.delete(type);
      }
    } catch (Exception e) {
      throw processException(e);
    } finally {
      closeSession(session);
    }

    if (type != null) {
      clearCache(type);
    }

    return type;
  }
  /**
   * Clears the cache for the type.
   *
   * <p>The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link
   * com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
   */
  @Override
  public void clearCache(Type type) {
    EntityCacheUtil.removeResult(
        TypeModelImpl.ENTITY_CACHE_ENABLED, TypeImpl.class, type.getPrimaryKey());

    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
  }
  @Override
  public Type updateImpl(Type type) {
    type = toUnwrappedModel(type);

    boolean isNew = type.isNew();

    Session session = null;

    try {
      session = openSession();

      if (type.isNew()) {
        session.save(type);

        type.setNew(false);
      } else {
        type = (Type) session.merge(type);
      }
    } catch (Exception e) {
      throw processException(e);
    } finally {
      closeSession(session);
    }

    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

    if (isNew) {
      finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    entityCache.putResult(
        TypeModelImpl.ENTITY_CACHE_ENABLED, TypeImpl.class, type.getPrimaryKey(), type, false);

    type.resetOriginalValues();

    return type;
  }
  @Override
  public Map<Serializable, Type> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
    if (primaryKeys.isEmpty()) {
      return Collections.emptyMap();
    }

    Map<Serializable, Type> map = new HashMap<Serializable, Type>();

    if (primaryKeys.size() == 1) {
      Iterator<Serializable> iterator = primaryKeys.iterator();

      Serializable primaryKey = iterator.next();

      Type type = fetchByPrimaryKey(primaryKey);

      if (type != null) {
        map.put(primaryKey, type);
      }

      return map;
    }

    Set<Serializable> uncachedPrimaryKeys = null;

    for (Serializable primaryKey : primaryKeys) {
      Type type =
          (Type)
              entityCache.getResult(TypeModelImpl.ENTITY_CACHE_ENABLED, TypeImpl.class, primaryKey);

      if (type == null) {
        if (uncachedPrimaryKeys == null) {
          uncachedPrimaryKeys = new HashSet<Serializable>();
        }

        uncachedPrimaryKeys.add(primaryKey);
      } else {
        map.put(primaryKey, type);
      }
    }

    if (uncachedPrimaryKeys == null) {
      return map;
    }

    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);

    query.append(_SQL_SELECT_TYPE_WHERE_PKS_IN);

    for (Serializable primaryKey : uncachedPrimaryKeys) {
      query.append(String.valueOf(primaryKey));

      query.append(StringPool.COMMA);
    }

    query.setIndex(query.index() - 1);

    query.append(StringPool.CLOSE_PARENTHESIS);

    String sql = query.toString();

    Session session = null;

    try {
      session = openSession();

      Query q = session.createQuery(sql);

      for (Type type : (List<Type>) q.list()) {
        map.put(type.getPrimaryKeyObj(), type);

        cacheResult(type);

        uncachedPrimaryKeys.remove(type.getPrimaryKeyObj());
      }

      for (Serializable primaryKey : uncachedPrimaryKeys) {
        entityCache.putResult(
            TypeModelImpl.ENTITY_CACHE_ENABLED, TypeImpl.class, primaryKey, _nullType);
      }
    } catch (Exception e) {
      throw processException(e);
    } finally {
      closeSession(session);
    }

    return map;
  }