001package org.apache.fulcrum.security.torque.basic;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020import java.sql.Connection;
021import java.util.List;
022
023import org.apache.fulcrum.security.entity.Group;
024import org.apache.fulcrum.security.torque.TorqueAbstractGroupManager;
025import org.apache.fulcrum.security.torque.om.TorqueBasicGroupPeer;
026import org.apache.torque.NoRowsException;
027import org.apache.torque.TooManyRowsException;
028import org.apache.torque.TorqueException;
029import org.apache.torque.criteria.Criteria;
030
031/**
032 * This implementation persists to a database via Torque.
033 *
034 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
035 * @version $Id:$
036 */
037public class TorqueBasicGroupManagerImpl extends TorqueAbstractGroupManager
038{
039    /**
040     * @see org.apache.fulcrum.security.torque.TorqueAbstractGroupManager#doSelectAllGroups(java.sql.Connection)
041     */
042    @SuppressWarnings("unchecked")
043        protected <T extends Group> List<T> doSelectAllGroups(Connection con)
044        throws TorqueException
045    {
046        Criteria criteria = new Criteria(TorqueBasicGroupPeer.DATABASE_NAME);
047
048        return (List<T>)TorqueBasicGroupPeer.doSelect(criteria, con);
049    }
050
051    /**
052     * @see org.apache.fulcrum.security.torque.TorqueAbstractGroupManager#doSelectById(java.lang.Integer, java.sql.Connection)
053     */
054    @SuppressWarnings("unchecked")
055        protected <T extends Group> T doSelectById(Integer id, Connection con)
056        throws NoRowsException, TooManyRowsException, TorqueException
057    {
058        return (T)TorqueBasicGroupPeer.retrieveByPK(id, con);
059    }
060
061    /**
062     * @see org.apache.fulcrum.security.torque.TorqueAbstractGroupManager#doSelectByName(java.lang.String, java.sql.Connection)
063     */
064    @SuppressWarnings("unchecked")
065        protected <T extends Group> T doSelectByName(String name, Connection con)
066        throws NoRowsException, TooManyRowsException, TorqueException
067    {
068        Criteria criteria = new Criteria(TorqueBasicGroupPeer.DATABASE_NAME);
069        criteria.where(TorqueBasicGroupPeer.GROUP_NAME, name);
070        criteria.setIgnoreCase(true);
071        criteria.setSingleRecord(true);
072
073        T t = (T)TorqueBasicGroupPeer.doSelectSingleRecord(criteria, con);
074        if (t == null)
075        {
076            throw new NoRowsException(name);
077        }
078
079        return t;
080    }
081}