001package org.apache.fulcrum.security.torque.dynamic;
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.TorqueDynamicGroup;
026import org.apache.fulcrum.security.torque.om.TorqueDynamicGroupPeer;
027import org.apache.torque.NoRowsException;
028import org.apache.torque.TooManyRowsException;
029import org.apache.torque.TorqueException;
030import org.apache.torque.criteria.Criteria;
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 TorqueDynamicGroupManagerImpl extends TorqueAbstractGroupManager
038{
039    /* (non-Javadoc)
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) throws TorqueException
044    {
045        Criteria criteria = new Criteria(TorqueDynamicGroupPeer.DATABASE_NAME);
046
047        return (List<T>)TorqueDynamicGroupPeer.doSelect(criteria, con);
048    }
049
050    /* (non-Javadoc)
051     * @see org.apache.fulcrum.security.torque.TorqueAbstractGroupManager#doSelectById(java.lang.Integer, java.sql.Connection)
052     */
053    @SuppressWarnings("unchecked")
054        protected <T extends Group> T doSelectById(Integer id, Connection con) throws NoRowsException, TooManyRowsException, TorqueException
055    {
056        return (T) TorqueDynamicGroupPeer.retrieveByPK(id, con);
057    }
058
059    /* (non-Javadoc)
060     * @see org.apache.fulcrum.security.torque.TorqueAbstractGroupManager#doSelectByName(java.lang.String, java.sql.Connection)
061     */
062    @SuppressWarnings("unchecked")
063        protected <T extends Group> T doSelectByName(String name, Connection con) throws NoRowsException, TooManyRowsException, TorqueException
064    {
065        Criteria criteria = new Criteria(TorqueDynamicGroupPeer.DATABASE_NAME);
066        criteria.where(TorqueDynamicGroupPeer.GROUP_NAME, name);
067        criteria.setIgnoreCase(true);
068        criteria.setSingleRecord(true);
069
070        List<TorqueDynamicGroup> groups = TorqueDynamicGroupPeer.doSelect(criteria, con);
071
072        if (groups.isEmpty())
073        {
074            throw new NoRowsException(name);
075        }
076
077        return (T) groups.get(0);
078    }
079}