/**
   * Creates a new web d a v props with the primary key. Does not add the web d a v props to the
   * database.
   *
   * @param webDavPropsId the primary key for the new web d a v props
   * @return the new web d a v props
   */
  @Override
  public WebDAVProps create(long webDavPropsId) {
    WebDAVProps webDAVProps = new WebDAVPropsImpl();

    webDAVProps.setNew(true);
    webDAVProps.setPrimaryKey(webDavPropsId);

    webDAVProps.setCompanyId(companyProvider.getCompanyId());

    return webDAVProps;
  }
  @Override
  public void clearCache(List<WebDAVProps> webDAVPropses) {
    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

    for (WebDAVProps webDAVProps : webDAVPropses) {
      entityCache.removeResult(
          WebDAVPropsModelImpl.ENTITY_CACHE_ENABLED,
          WebDAVPropsImpl.class,
          webDAVProps.getPrimaryKey());

      clearUniqueFindersCache((WebDAVPropsModelImpl) webDAVProps);
    }
  }
 /**
  * Caches the web d a v propses in the entity cache if it is enabled.
  *
  * @param webDAVPropses the web d a v propses
  */
 @Override
 public void cacheResult(List<WebDAVProps> webDAVPropses) {
   for (WebDAVProps webDAVProps : webDAVPropses) {
     if (entityCache.getResult(
             WebDAVPropsModelImpl.ENTITY_CACHE_ENABLED,
             WebDAVPropsImpl.class,
             webDAVProps.getPrimaryKey())
         == null) {
       cacheResult(webDAVProps);
     } else {
       webDAVProps.resetOriginalValues();
     }
   }
 }
  /**
   * Caches the web d a v props in the entity cache if it is enabled.
   *
   * @param webDAVProps the web d a v props
   */
  @Override
  public void cacheResult(WebDAVProps webDAVProps) {
    entityCache.putResult(
        WebDAVPropsModelImpl.ENTITY_CACHE_ENABLED,
        WebDAVPropsImpl.class,
        webDAVProps.getPrimaryKey(),
        webDAVProps);

    finderCache.putResult(
        FINDER_PATH_FETCH_BY_C_C,
        new Object[] {webDAVProps.getClassNameId(), webDAVProps.getClassPK()},
        webDAVProps);

    webDAVProps.resetOriginalValues();
  }
  @Override
  protected WebDAVProps removeImpl(WebDAVProps webDAVProps) {
    webDAVProps = toUnwrappedModel(webDAVProps);

    Session session = null;

    try {
      session = openSession();

      if (!session.contains(webDAVProps)) {
        webDAVProps =
            (WebDAVProps) session.get(WebDAVPropsImpl.class, webDAVProps.getPrimaryKeyObj());
      }

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

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

    return webDAVProps;
  }
  protected WebDAVProps toUnwrappedModel(WebDAVProps webDAVProps) {
    if (webDAVProps instanceof WebDAVPropsImpl) {
      return webDAVProps;
    }

    WebDAVPropsImpl webDAVPropsImpl = new WebDAVPropsImpl();

    webDAVPropsImpl.setNew(webDAVProps.isNew());
    webDAVPropsImpl.setPrimaryKey(webDAVProps.getPrimaryKey());

    webDAVPropsImpl.setMvccVersion(webDAVProps.getMvccVersion());
    webDAVPropsImpl.setWebDavPropsId(webDAVProps.getWebDavPropsId());
    webDAVPropsImpl.setCompanyId(webDAVProps.getCompanyId());
    webDAVPropsImpl.setCreateDate(webDAVProps.getCreateDate());
    webDAVPropsImpl.setModifiedDate(webDAVProps.getModifiedDate());
    webDAVPropsImpl.setClassNameId(webDAVProps.getClassNameId());
    webDAVPropsImpl.setClassPK(webDAVProps.getClassPK());
    webDAVPropsImpl.setProps(webDAVProps.getProps());

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

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

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

      Serializable primaryKey = iterator.next();

      WebDAVProps webDAVProps = fetchByPrimaryKey(primaryKey);

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

      return map;
    }

    Set<Serializable> uncachedPrimaryKeys = null;

    for (Serializable primaryKey : primaryKeys) {
      WebDAVProps webDAVProps =
          (WebDAVProps)
              entityCache.getResult(
                  WebDAVPropsModelImpl.ENTITY_CACHE_ENABLED, WebDAVPropsImpl.class, primaryKey);

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

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

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

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

    query.append(_SQL_SELECT_WEBDAVPROPS_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 (WebDAVProps webDAVProps : (List<WebDAVProps>) q.list()) {
        map.put(webDAVProps.getPrimaryKeyObj(), webDAVProps);

        cacheResult(webDAVProps);

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

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

    return map;
  }
  @Override
  public WebDAVProps updateImpl(WebDAVProps webDAVProps) {
    webDAVProps = toUnwrappedModel(webDAVProps);

    boolean isNew = webDAVProps.isNew();

    WebDAVPropsModelImpl webDAVPropsModelImpl = (WebDAVPropsModelImpl) webDAVProps;

    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

    Date now = new Date();

    if (isNew && (webDAVProps.getCreateDate() == null)) {
      if (serviceContext == null) {
        webDAVProps.setCreateDate(now);
      } else {
        webDAVProps.setCreateDate(serviceContext.getCreateDate(now));
      }
    }

    if (!webDAVPropsModelImpl.hasSetModifiedDate()) {
      if (serviceContext == null) {
        webDAVProps.setModifiedDate(now);
      } else {
        webDAVProps.setModifiedDate(serviceContext.getModifiedDate(now));
      }
    }

    Session session = null;

    try {
      session = openSession();

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

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

    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

    if (isNew || !WebDAVPropsModelImpl.COLUMN_BITMASK_ENABLED) {
      finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    entityCache.putResult(
        WebDAVPropsModelImpl.ENTITY_CACHE_ENABLED,
        WebDAVPropsImpl.class,
        webDAVProps.getPrimaryKey(),
        webDAVProps,
        false);

    clearUniqueFindersCache(webDAVPropsModelImpl);
    cacheUniqueFindersCache(webDAVPropsModelImpl, isNew);

    webDAVProps.resetOriginalValues();

    return webDAVProps;
  }
  /**
   * Returns the web d a v props where classNameId = &#63; and classPK = &#63; or returns <code>null
   * </code> if it could not be found, optionally using the finder cache.
   *
   * @param classNameId the class name ID
   * @param classPK the class p k
   * @param retrieveFromCache whether to retrieve from the finder cache
   * @return the matching web d a v props, or <code>null</code> if a matching web d a v props could
   *     not be found
   */
  @Override
  public WebDAVProps fetchByC_C(long classNameId, long classPK, boolean retrieveFromCache) {
    Object[] finderArgs = new Object[] {classNameId, classPK};

    Object result = null;

    if (retrieveFromCache) {
      result = finderCache.getResult(FINDER_PATH_FETCH_BY_C_C, finderArgs, this);
    }

    if (result instanceof WebDAVProps) {
      WebDAVProps webDAVProps = (WebDAVProps) result;

      if ((classNameId != webDAVProps.getClassNameId()) || (classPK != webDAVProps.getClassPK())) {
        result = null;
      }
    }

    if (result == null) {
      StringBundler query = new StringBundler(4);

      query.append(_SQL_SELECT_WEBDAVPROPS_WHERE);

      query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);

      query.append(_FINDER_COLUMN_C_C_CLASSPK_2);

      String sql = query.toString();

      Session session = null;

      try {
        session = openSession();

        Query q = session.createQuery(sql);

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(classNameId);

        qPos.add(classPK);

        List<WebDAVProps> list = q.list();

        if (list.isEmpty()) {
          finderCache.putResult(FINDER_PATH_FETCH_BY_C_C, finderArgs, list);
        } else {
          WebDAVProps webDAVProps = list.get(0);

          result = webDAVProps;

          cacheResult(webDAVProps);

          if ((webDAVProps.getClassNameId() != classNameId)
              || (webDAVProps.getClassPK() != classPK)) {
            finderCache.putResult(FINDER_PATH_FETCH_BY_C_C, finderArgs, webDAVProps);
          }
        }
      } catch (Exception e) {
        finderCache.removeResult(FINDER_PATH_FETCH_BY_C_C, finderArgs);

        throw processException(e);
      } finally {
        closeSession(session);
      }
    }

    if (result instanceof List<?>) {
      return null;
    } else {
      return (WebDAVProps) result;
    }
  }