001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/search/tags/sakai_2-4-1/search-impl/impl/src/java/org/sakaiproject/search/component/dao/impl/SearchBuilderItemDaoImpl.java $
003: * $Id: SearchBuilderItemDaoImpl.java 29601 2007-04-26 13:53:34Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.search.component.dao.impl;
021:
022: import java.sql.Connection;
023: import java.sql.PreparedStatement;
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026: import java.text.MessageFormat;
027: import java.util.List;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.hibernate.HibernateException;
032: import org.hibernate.Session;
033: import org.hibernate.criterion.Expression;
034: import org.sakaiproject.search.dao.SearchBuilderItemDao;
035: import org.sakaiproject.search.model.SearchBuilderItem;
036: import org.sakaiproject.search.model.impl.SearchBuilderItemImpl;
037: import org.springframework.orm.hibernate3.HibernateCallback;
038: import org.springframework.orm.hibernate3.HibernateTemplate;
039: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
040: import org.springframework.transaction.support.TransactionSynchronizationManager;
041:
042: /**
043: * @author ieb
044: */
045: public class SearchBuilderItemDaoImpl extends HibernateDaoSupport
046: implements SearchBuilderItemDao {
047:
048: /**
049: * debug logger
050: */
051: private static Log dlog = LogFactory
052: .getLog(SearchBuilderItemDaoImpl.class);
053:
054: /**
055: * create a new search builder item {@inheritDoc}
056: */
057: public SearchBuilderItem create() {
058: return new SearchBuilderItemImpl();
059: }
060:
061: /**
062: * update a search Builder item
063: *
064: * @{inheritDoc}
065: */
066: public void update(SearchBuilderItem sb) {
067: HibernateTemplate ht = getHibernateTemplate();
068: ht.saveOrUpdate(sb);
069: ht.flush();
070:
071: }
072:
073: /**
074: * reset all the documents to cause a reload of current state
075: *
076: * @{inheritDoc}
077: */
078: public List getAll() {
079: HibernateCallback callback = new HibernateCallback() {
080: public Object doInHibernate(Session session)
081: throws HibernateException {
082: return session.createCriteria(
083: SearchBuilderItemImpl.class).list();
084: }
085: };
086:
087: return (List) getHibernateTemplate().execute(callback);
088: }
089:
090: /**
091: * locate a document by name, to modify it
092: *
093: * @{inheritDoc}
094: */
095: public SearchBuilderItem findByName(final String resourceName) {
096: if (resourceName != null && resourceName.length() > 255) {
097: dlog.warn("Entity Reference longer than 255 characters :"
098: + resourceName);
099: return null;
100: }
101: HibernateCallback callback = new HibernateCallback() {
102: public Object doInHibernate(Session session)
103: throws HibernateException {
104: return session.createCriteria(
105: SearchBuilderItemImpl.class).add(
106: Expression.eq("name", resourceName)).list();
107: }
108:
109: };
110: List l = (List) getHibernateTemplate().execute(callback);
111: if (l.size() == 0) {
112: return null;
113: } else {
114: return (SearchBuilderItem) l.get(0);
115: }
116:
117: }
118:
119: private int countPending(Connection connection) {
120:
121: PreparedStatement pst = null;
122: ResultSet rst = null;
123: try {
124: pst = connection
125: .prepareStatement("select count(*) from searchbuilderitem where searchstate = ? and searchaction <> ?");
126: pst.clearParameters();
127: pst.setInt(1, SearchBuilderItem.STATE_PENDING.intValue());
128: pst.setInt(2, SearchBuilderItem.ACTION_UNKNOWN.intValue());
129: rst = pst.executeQuery();
130: if (rst.next()) {
131: return rst.getInt(1);
132: }
133: return 0;
134: } catch (SQLException sqlex) {
135: return 0;
136: } finally {
137: try {
138: pst.close();
139: } catch (Exception ex) {
140: }
141: ;
142: }
143:
144: }
145:
146: public int countPending() {
147:
148: HibernateCallback callback = new HibernateCallback() {
149: public Object doInHibernate(Session session)
150: throws HibernateException {
151: return new Integer(countPending(session.connection()));
152: }
153: };
154:
155: return ((Integer) getHibernateTemplate().execute(callback))
156: .intValue();
157: }
158:
159: public List getGlobalMasters() {
160: HibernateCallback callback = new HibernateCallback() {
161: public Object doInHibernate(Session session)
162: throws HibernateException {
163: return session.createCriteria(
164: SearchBuilderItemImpl.class).add(
165: Expression.eq("name",
166: SearchBuilderItem.GLOBAL_MASTER))
167: .list();
168: }
169: };
170:
171: return (List) getHibernateTemplate().execute(callback);
172: }
173:
174: public List getSiteMasters() {
175: HibernateCallback callback = new HibernateCallback() {
176: public Object doInHibernate(Session session)
177: throws HibernateException {
178: return session
179: .createCriteria(SearchBuilderItemImpl.class)
180: .add(
181: Expression
182: .like(
183: "name",
184: SearchBuilderItem.SITE_MASTER_PATTERN))
185: .add(
186: Expression
187: .not(Expression
188: .eq(
189: "context",
190: SearchBuilderItem.GLOBAL_CONTEXT)))
191: .list();
192: }
193: };
194:
195: return (List) getHibernateTemplate().execute(callback);
196: }
197: }
|