001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/facade/authz/AuthorizationFacadeQueries.java $
003: * $Id: AuthorizationFacadeQueries.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.tool.assessment.facade.authz;
021:
022: import java.sql.SQLException;
023: import java.util.ArrayList;
024: import java.util.Date;
025: import java.util.List;
026:
027: import org.springframework.orm.hibernate3.HibernateCallback;
028: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
029: import org.hibernate.HibernateException;
030: import org.hibernate.Query;
031: import org.hibernate.Session;
032:
033: import org.sakaiproject.tool.assessment.data.dao.authz.AuthorizationData;
034: import org.sakaiproject.tool.assessment.data.dao.authz.QualifierData;
035: import org.sakaiproject.tool.assessment.data.ifc.authz.AuthorizationIfc;
036: import org.sakaiproject.tool.assessment.data.ifc.authz.QualifierIfc;
037: import org.sakaiproject.tool.assessment.facade.DataFacadeException;
038: import org.sakaiproject.tool.assessment.facade.authz.AuthorizationFacade;
039: import org.sakaiproject.tool.assessment.facade.authz.QualifierFacade;
040: import org.sakaiproject.tool.assessment.services.PersistenceService;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044:
045: public class AuthorizationFacadeQueries extends HibernateDaoSupport
046: implements AuthorizationFacadeQueriesAPI {
047:
048: private static Log log = LogFactory
049: .getLog(AuthorizationFacadeQueries.class);
050:
051: public AuthorizationFacadeQueries() {
052: }
053:
054: public QualifierIteratorFacade getQualifierParents(
055: final String qualifierId) {
056: final HibernateCallback hcb = new HibernateCallback() {
057: public Object doInHibernate(Session session)
058: throws HibernateException, SQLException {
059: Query q = session
060: .createQuery("select p from QualifierData as p, QualifierData as c, QualifierHierarchyData as q where p.qualifierId=q.parentId and c.qualifierId=q.childId and q.childId=?");
061: q.setString(0, qualifierId);
062: return q.list();
063: };
064: };
065: List parents = getHibernateTemplate().executeFind(hcb);
066:
067: // List parents = getHibernateTemplate().find(
068: // "select p from QualifierData as p, QualifierData as c, QualifierHierarchyData as q where p.qualifierId=q.parentId and c.qualifierId=q.childId and q.childId=?",
069: // new Object[] {qualifierId}
070: // ,
071: // new org.hibernate.type.Type[] {Hibernate.STRING});
072: // turn them to Facade
073: ArrayList a = new ArrayList();
074: for (int i = 0; i < parents.size(); i++) {
075: QualifierData data = (QualifierData) parents.get(i);
076: QualifierFacade qf = new QualifierFacade(data);
077: a.add(qf);
078: }
079: return new QualifierIteratorFacade(a);
080: }
081:
082: public QualifierIteratorFacade getQualifierChildren(
083: final String qualifierId) {
084: final HibernateCallback hcb = new HibernateCallback() {
085: public Object doInHibernate(Session session)
086: throws HibernateException, SQLException {
087: Query q = session
088: .createQuery("select p from QualifierData as p, QualifierData as c, QualifierHierarchyData as q where p.qualifierId=q.parentId and c.qualifierId=q.childId and q.parentId=?");
089: q.setString(0, qualifierId);
090: return q.list();
091: };
092: };
093: List children = getHibernateTemplate().executeFind(hcb);
094:
095: // List children = getHibernateTemplate().find(
096: // "select p from QualifierData as p, QualifierData as c, QualifierHierarchyData as q where p.qualifierId=q.parentId and c.qualifierId=q.childId and q.parentId=?",
097: // new Object[] {qualifierId},
098: // new org.hibernate.type.Type[] {Hibernate.STRING});
099: // turn them to Facade
100: ArrayList a = new ArrayList();
101: for (int i = 0; i < children.size(); i++) {
102: QualifierData data = (QualifierData) children.get(i);
103: QualifierFacade qf = new QualifierFacade(data);
104: a.add(qf);
105: }
106: return new QualifierIteratorFacade(a);
107: }
108:
109: public void showQualifiers(QualifierIteratorFacade iter) {
110: while (iter.hasNextQualifier()) {
111: //QualifierFacade q = (QualifierFacade)iter.nextQualifier();
112: }
113: }
114:
115: public void addAuthz(AuthorizationIfc a) {
116: AuthorizationData data;
117: if (a instanceof AuthorizationFacade)
118: data = (AuthorizationData) ((AuthorizationFacade) a)
119: .getData();
120: else
121: data = (AuthorizationData) a;
122:
123: int retryCount = PersistenceService.getInstance()
124: .getRetryCount().intValue();
125: while (retryCount > 0) {
126: try {
127: getHibernateTemplate().save(data);
128: retryCount = 0;
129: } catch (Exception e) {
130: log.warn("problem adding authorization: "
131: + e.getMessage());
132: retryCount = PersistenceService.getInstance()
133: .retryDeadlock(e, retryCount);
134: }
135: }
136: }
137:
138: public void addQualifier(QualifierIfc q) {
139: QualifierData data;
140: if (q instanceof QualifierFacade)
141: data = (QualifierData) ((QualifierFacade) q).getData();
142: else
143: data = (QualifierData) q;
144:
145: int retryCount = PersistenceService.getInstance()
146: .getRetryCount().intValue();
147: while (retryCount > 0) {
148: try {
149: getHibernateTemplate().save(data);
150: retryCount = 0;
151: } catch (Exception e) {
152: log.warn("problem adding Qualifier: " + e.getMessage());
153: retryCount = PersistenceService.getInstance()
154: .retryDeadlock(e, retryCount);
155: }
156: }
157: }
158:
159: public static void main(String[] args) throws DataFacadeException {
160: AuthorizationFacadeQueriesAPI instance = new AuthorizationFacadeQueries();
161: if (args[0].equals("listChild")) {
162: QualifierIteratorFacade childrenIter = instance
163: .getQualifierChildren(args[1]);
164: instance.showQualifiers(childrenIter);
165: }
166: if (args[0].equals("addAuthz")) {
167: AuthorizationFacade a = new AuthorizationFacade(args[1],
168: args[2], args[3], new Date(), null, "2",
169: new Date(), Boolean.TRUE);
170: instance.addAuthz(a);
171: }
172: System.exit(0);
173: }
174:
175: }
|