/** * Caches the foo in the entity cache if it is enabled. * * @param foo the foo */ @Override public void cacheResult(Foo foo) { EntityCacheUtil.putResult( FooModelImpl.ENTITY_CACHE_ENABLED, FooImpl.class, foo.getPrimaryKey(), foo); foo.resetOriginalValues(); }
/** * Creates a new foo with the primary key. Does not add the foo to the database. * * @param fooId the primary key for the new foo * @return the new foo */ @Override public Foo create(long fooId) { Foo foo = new FooImpl(); foo.setNew(true); foo.setPrimaryKey(fooId); return foo; }
@Override public void clearCache(List<Foo> foos) { FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); for (Foo foo : foos) { EntityCacheUtil.removeResult( FooModelImpl.ENTITY_CACHE_ENABLED, FooImpl.class, foo.getPrimaryKey()); } }
@Override public Foo updateImpl(com.liferay.testpacl.model.Foo foo) { foo = toUnwrappedModel(foo); boolean isNew = foo.isNew(); FooModelImpl fooModelImpl = (FooModelImpl) foo; Session session = null; try { session = openSession(); if (foo.isNew()) { session.save(foo); foo.setNew(false); } else { session.merge(foo); } } catch (Exception e) { throw processException(e); } finally { closeSession(session); } FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION); if (isNew || !FooModelImpl.COLUMN_BITMASK_ENABLED) { FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION); } else { if ((fooModelImpl.getColumnBitmask() & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_FIELD2.getColumnBitmask()) != 0) { Object[] args = new Object[] {fooModelImpl.getOriginalField2()}; FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_FIELD2, args); FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_FIELD2, args); args = new Object[] {fooModelImpl.getField2()}; FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_FIELD2, args); FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_FIELD2, args); } } EntityCacheUtil.putResult( FooModelImpl.ENTITY_CACHE_ENABLED, FooImpl.class, foo.getPrimaryKey(), foo, false); foo.resetOriginalValues(); return foo; }
/** * Caches the foos in the entity cache if it is enabled. * * @param foos the foos */ @Override public void cacheResult(List<Foo> foos) { for (Foo foo : foos) { if (EntityCacheUtil.getResult( FooModelImpl.ENTITY_CACHE_ENABLED, FooImpl.class, foo.getPrimaryKey()) == null) { cacheResult(foo); } else { foo.resetOriginalValues(); } } }
@Override protected Foo removeImpl(Foo foo) { foo = toUnwrappedModel(foo); Session session = null; try { session = openSession(); if (!session.contains(foo)) { foo = (Foo) session.get(FooImpl.class, foo.getPrimaryKeyObj()); } if (foo != null) { session.delete(foo); } } catch (Exception e) { throw processException(e); } finally { closeSession(session); } if (foo != null) { clearCache(foo); } return foo; }
public int compareTo(Foo foo) { int value = 0; value = getField1().compareTo(foo.getField1()); if (value != 0) { return value; } return 0; }
@Override public boolean equals(Object obj) { if (obj == null) { return false; } Foo foo = null; try { foo = (Foo) obj; } catch (ClassCastException cce) { return false; } long primaryKey = foo.getPrimaryKey(); if (getPrimaryKey() == primaryKey) { return true; } else { return false; } }
@Override public Map<Serializable, Foo> fetchByPrimaryKeys(Set<Serializable> primaryKeys) { if (primaryKeys.isEmpty()) { return Collections.emptyMap(); } Map<Serializable, Foo> map = new HashMap<Serializable, Foo>(); if (primaryKeys.size() == 1) { Iterator<Serializable> iterator = primaryKeys.iterator(); Serializable primaryKey = iterator.next(); Foo foo = fetchByPrimaryKey(primaryKey); if (foo != null) { map.put(primaryKey, foo); } return map; } Set<Serializable> uncachedPrimaryKeys = null; for (Serializable primaryKey : primaryKeys) { Foo foo = (Foo) EntityCacheUtil.getResult( FooModelImpl.ENTITY_CACHE_ENABLED, FooImpl.class, primaryKey); if (foo == null) { if (uncachedPrimaryKeys == null) { uncachedPrimaryKeys = new HashSet<Serializable>(); } uncachedPrimaryKeys.add(primaryKey); } else { map.put(primaryKey, foo); } } if (uncachedPrimaryKeys == null) { return map; } StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1); query.append(_SQL_SELECT_FOO_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 (Foo foo : (List<Foo>) q.list()) { map.put(foo.getPrimaryKeyObj(), foo); cacheResult(foo); uncachedPrimaryKeys.remove(foo.getPrimaryKeyObj()); } for (Serializable primaryKey : uncachedPrimaryKeys) { EntityCacheUtil.putResult( FooModelImpl.ENTITY_CACHE_ENABLED, FooImpl.class, primaryKey, _nullFoo); } } catch (Exception e) { throw processException(e); } finally { closeSession(session); } return map; }
protected Foo toUnwrappedModel(Foo foo) { if (foo instanceof FooImpl) { return foo; } FooImpl fooImpl = new FooImpl(); fooImpl.setNew(foo.isNew()); fooImpl.setPrimaryKey(foo.getPrimaryKey()); fooImpl.setFooId(foo.getFooId()); fooImpl.setGroupId(foo.getGroupId()); fooImpl.setCompanyId(foo.getCompanyId()); fooImpl.setUserId(foo.getUserId()); fooImpl.setUserName(foo.getUserName()); fooImpl.setCreateDate(foo.getCreateDate()); fooImpl.setModifiedDate(foo.getModifiedDate()); fooImpl.setField1(foo.getField1()); fooImpl.setField2(foo.isField2()); fooImpl.setField3(foo.getField3()); fooImpl.setField4(foo.getField4()); fooImpl.setField5(foo.getField5()); return fooImpl; }
/** * Returns an ordered range of all the foos where field2 = ?. * * <p>Useful when paginating results. Returns a maximum of <code>end - start</code> instances. * <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result * set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start * </code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} * will return the full result set. If <code>orderByComparator</code> is specified, then the query * will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and * pagination is required (<code>start</code> and <code>end</code> are not {@link * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default * ORDER BY logic from {@link com.liferay.testpacl.model.impl.FooModelImpl}. If both <code> * orderByComparator</code> and pagination are absent, for performance reasons, the query will not * have an ORDER BY clause and the returned result set will be sorted on by the primary key in an * ascending order. * * @param field2 the field2 * @param start the lower bound of the range of foos * @param end the upper bound of the range of foos (not inclusive) * @param orderByComparator the comparator to order the results by (optionally <code>null</code>) * @return the ordered range of matching foos */ @Override public List<Foo> findByField2( boolean field2, int start, int end, OrderByComparator<Foo> orderByComparator) { boolean pagination = true; FinderPath finderPath = null; Object[] finderArgs = null; if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) { pagination = false; finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_FIELD2; finderArgs = new Object[] {field2}; } else { finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_FIELD2; finderArgs = new Object[] {field2, start, end, orderByComparator}; } List<Foo> list = (List<Foo>) FinderCacheUtil.getResult(finderPath, finderArgs, this); if ((list != null) && !list.isEmpty()) { for (Foo foo : list) { if ((field2 != foo.getField2())) { list = null; break; } } } if (list == null) { StringBundler query = null; if (orderByComparator != null) { query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 3)); } else { query = new StringBundler(3); } query.append(_SQL_SELECT_FOO_WHERE); query.append(_FINDER_COLUMN_FIELD2_FIELD2_2); if (orderByComparator != null) { appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator); } else if (pagination) { query.append(FooModelImpl.ORDER_BY_JPQL); } String sql = query.toString(); Session session = null; try { session = openSession(); Query q = session.createQuery(sql); QueryPos qPos = QueryPos.getInstance(q); qPos.add(field2); if (!pagination) { list = (List<Foo>) QueryUtil.list(q, getDialect(), start, end, false); Collections.sort(list); list = Collections.unmodifiableList(list); } else { list = (List<Foo>) QueryUtil.list(q, getDialect(), start, end); } cacheResult(list); FinderCacheUtil.putResult(finderPath, finderArgs, list); } catch (Exception e) { FinderCacheUtil.removeResult(finderPath, finderArgs); throw processException(e); } finally { closeSession(session); } } return list; }