001    /**
002     * Copyright (c) 2000-2013 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.security.pacl.DoPrivileged;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import org.hibernate.criterion.Projections;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    @DoPrivileged
030    public class ProjectionFactoryImpl implements ProjectionFactory {
031    
032            public Projection alias(Projection projection, String alias) {
033                    ProjectionImpl projectionImpl = (ProjectionImpl)projection;
034    
035                    return new ProjectionImpl(
036                            Projections.alias(projectionImpl.getWrappedProjection(), alias));
037            }
038    
039            public Projection avg(String propertyName) {
040                    return new ProjectionImpl(Projections.avg(propertyName));
041            }
042    
043            public Projection count(String propertyName) {
044                    return new ProjectionImpl(Projections.count(propertyName));
045            }
046    
047            public Projection countDistinct(String propertyName) {
048                    return new ProjectionImpl(Projections.countDistinct(propertyName));
049            }
050    
051            public Projection distinct(Projection projection) {
052                    ProjectionImpl projectionImpl = (ProjectionImpl)projection;
053    
054                    return new ProjectionImpl(
055                            Projections.distinct(projectionImpl.getWrappedProjection()));
056            }
057    
058            public Projection groupProperty(String propertyName) {
059                    return new ProjectionImpl(Projections.groupProperty(propertyName));
060            }
061    
062            public Projection max(String propertyName) {
063                    return new ProjectionImpl(Projections.max(propertyName));
064            }
065    
066            public Projection min(String propertyName) {
067                    return new ProjectionImpl(Projections.min(propertyName));
068            }
069    
070            public ProjectionList projectionList() {
071                    return new ProjectionListImpl(Projections.projectionList());
072            }
073    
074            public Projection property(String propertyName) {
075                    return new ProjectionImpl(Projections.property(propertyName));
076            }
077    
078            public Projection rowCount() {
079                    return new ProjectionImpl(Projections.rowCount());
080            }
081    
082            public Projection sqlProjection(
083                    String sql, String[] columnAliases, Type[] types) {
084    
085                    if (Validator.isNull(types)) {
086                            return new ProjectionImpl(
087                                    Projections.sqlProjection(sql, columnAliases, null));
088                    }
089    
090                    org.hibernate.type.Type[] hibernateTypes =
091                            new org.hibernate.type.Type[types.length];
092    
093                    for (int i = 0; i < types.length; i++) {
094                            hibernateTypes[i] = TypeTranslator.translate(types[i]);
095                    }
096    
097                    return new ProjectionImpl(
098                            Projections.sqlProjection(sql, columnAliases, hibernateTypes));
099            }
100    
101            public Projection sum(String propertyName) {
102                    return new ProjectionImpl(Projections.sum(propertyName));
103            }
104    
105    }