001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.riot.hibernate.dao;
025:
026: import org.hibernate.Query;
027: import org.riotfamily.common.beans.PropertyUtils;
028: import org.riotfamily.riot.dao.CutAndPasteEnabledDao;
029: import org.riotfamily.riot.dao.ListParams;
030: import org.riotfamily.riot.dao.ParentChildDao;
031: import org.riotfamily.riot.hibernate.support.HibernateUtils;
032:
033: /**
034: * ParentChildDao implementation based on Hibernate.
035: */
036: public class HqlParentChildDao extends HqlDao implements
037: ParentChildDao, CutAndPasteEnabledDao {
038:
039: private String parentProperty;
040:
041: public String getParentProperty() {
042: return parentProperty;
043: }
044:
045: public void setParentProperty(String parentProperty) {
046: this .parentProperty = parentProperty;
047: }
048:
049: public Object getParent(Object entity) {
050: return PropertyUtils.getProperty(entity, parentProperty);
051: }
052:
053: public void save(Object entity, Object parent) {
054: PropertyUtils.setProperty(entity, parentProperty, parent);
055: super .save(entity, parent);
056: }
057:
058: public void delete(Object entity, Object parent) {
059: PropertyUtils.setProperty(entity, parentProperty, null);
060: super .delete(entity, parent);
061: }
062:
063: protected void setQueryParameters(Query query, Object parent,
064: ListParams params) {
065:
066: super .setQueryParameters(query, parent, params);
067: if (parent != null) {
068: query.setParameter("parent", parent);
069: }
070: }
071:
072: protected String getWhereClause(Object parent, ListParams params) {
073: StringBuffer sb = new StringBuffer();
074: if (parentProperty != null) {
075: sb.append("this.");
076: sb.append(parentProperty);
077: if (parent == null) {
078: sb.append(" is null");
079: } else {
080: sb.append(" = :parent ");
081: }
082: }
083: HibernateUtils.appendHql(sb, "and", super .getWhereClause(
084: parent, params));
085: return sb.toString();
086: }
087:
088: public void addChild(Object entity, Object parent) {
089: PropertyUtils.setProperty(entity, parentProperty, parent);
090: setPositionIfNeeded(entity, parent);
091: update(entity);
092: }
093:
094: /**
095: * This method does nothing. Before the parentProperty was set to null here.
096: * But since the only call is from the Clipboard's pasteCut method, which
097: * calls this after the addChild is called, setting the parentProperty to
098: * null won't have the desired effect.
099: */
100: public void removeChild(Object entity, Object parent) {
101: }
102:
103: }
|