001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.orm.hibernate;
016    
017    import com.liferay.portal.kernel.dao.orm.Projection;
018    import com.liferay.portal.kernel.dao.orm.ProjectionFactory;
019    import com.liferay.portal.kernel.dao.orm.ProjectionList;
020    import com.liferay.portal.kernel.dao.orm.Type;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import org.hibernate.criterion.Projections;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class ProjectionFactoryImpl implements ProjectionFactory {
029    
030            public Projection alias(Projection projection, String alias) {
031                    ProjectionImpl projectionImpl = (ProjectionImpl)projection;
032    
033                    return new ProjectionImpl(
034                            Projections.alias(projectionImpl.getWrappedProjection(), alias));
035            }
036    
037            public Projection avg(String propertyName) {
038                    return new ProjectionImpl(Projections.avg(propertyName));
039            }
040    
041            public Projection count(String propertyName) {
042                    return new ProjectionImpl(Projections.count(propertyName));
043            }
044    
045            public Projection countDistinct(String propertyName) {
046                    return new ProjectionImpl(Projections.countDistinct(propertyName));
047            }
048    
049            public Projection distinct(Projection projection) {
050                    ProjectionImpl projectionImpl = (ProjectionImpl)projection;
051    
052                    return new ProjectionImpl(
053                            Projections.distinct(projectionImpl.getWrappedProjection()));
054            }
055    
056            public Projection groupProperty(String propertyName) {
057                    return new ProjectionImpl(Projections.groupProperty(propertyName));
058            }
059    
060            public Projection max(String propertyName) {
061                    return new ProjectionImpl(Projections.max(propertyName));
062            }
063    
064            public Projection min(String propertyName) {
065                    return new ProjectionImpl(Projections.min(propertyName));
066            }
067    
068            public ProjectionList projectionList() {
069                    return new ProjectionListImpl(Projections.projectionList());
070            }
071    
072            public Projection property(String propertyName) {
073                    return new ProjectionImpl(Projections.property(propertyName));
074            }
075    
076            public Projection rowCount() {
077                    return new ProjectionImpl(Projections.rowCount());
078            }
079    
080            public Projection sqlProjection(
081                    String sql, String[] columnAliases, Type[] types) {
082    
083                    if (Validator.isNull(types)) {
084                            return new ProjectionImpl(
085                                    Projections.sqlProjection(sql, columnAliases, null));
086                    }
087    
088                    org.hibernate.type.Type[] hibernateTypes =
089                            new org.hibernate.type.Type[types.length];
090    
091                    for (int i = 0; i < types.length; i++) {
092                            hibernateTypes[i] = TypeTranslator.translate(types[i]);
093                    }
094    
095                    return new ProjectionImpl(
096                            Projections.sqlProjection(sql, columnAliases, hibernateTypes));
097            }
098    
099            public Projection sum(String propertyName) {
100                    return new ProjectionImpl(Projections.sum(propertyName));
101            }
102    
103    }