001    /**
002     * Copyright (c) 2000-present 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.ArrayUtil;
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            @Override
033            public Projection alias(Projection projection, String alias) {
034                    ProjectionImpl projectionImpl = (ProjectionImpl)projection;
035    
036                    return new ProjectionImpl(
037                            Projections.alias(projectionImpl.getWrappedProjection(), alias));
038            }
039    
040            @Override
041            public Projection avg(String propertyName) {
042                    return new ProjectionImpl(Projections.avg(propertyName));
043            }
044    
045            @Override
046            public Projection count(String propertyName) {
047                    return new ProjectionImpl(Projections.count(propertyName));
048            }
049    
050            @Override
051            public Projection countDistinct(String propertyName) {
052                    return new ProjectionImpl(Projections.countDistinct(propertyName));
053            }
054    
055            @Override
056            public Projection distinct(Projection projection) {
057                    ProjectionImpl projectionImpl = (ProjectionImpl)projection;
058    
059                    return new ProjectionImpl(
060                            Projections.distinct(projectionImpl.getWrappedProjection()));
061            }
062    
063            @Override
064            public Projection groupProperty(String propertyName) {
065                    return new ProjectionImpl(Projections.groupProperty(propertyName));
066            }
067    
068            @Override
069            public Projection max(String propertyName) {
070                    return new ProjectionImpl(Projections.max(propertyName));
071            }
072    
073            @Override
074            public Projection min(String propertyName) {
075                    return new ProjectionImpl(Projections.min(propertyName));
076            }
077    
078            @Override
079            public ProjectionList projectionList() {
080                    return new ProjectionListImpl(Projections.projectionList());
081            }
082    
083            @Override
084            public Projection property(String propertyName) {
085                    return new ProjectionImpl(Projections.property(propertyName));
086            }
087    
088            @Override
089            public Projection rowCount() {
090                    return new ProjectionImpl(Projections.rowCount());
091            }
092    
093            @Override
094            public Projection sqlGroupProjection(
095                    String sql, String groupBy, String[] columnAliases, Type[] types) {
096    
097                    if (ArrayUtil.isEmpty(types)) {
098                            return new ProjectionImpl(
099                                    Projections.sqlGroupProjection(
100                                            sql, groupBy, columnAliases, null));
101                    }
102    
103                    org.hibernate.type.Type[] hibernateTypes =
104                            new org.hibernate.type.Type[types.length];
105    
106                    for (int i = 0; i < types.length; i++) {
107                            hibernateTypes[i] = TypeTranslator.translate(types[i]);
108                    }
109    
110                    return new ProjectionImpl(
111                            Projections.sqlGroupProjection(
112                                    sql, groupBy, columnAliases, hibernateTypes));
113            }
114    
115            @Override
116            public Projection sqlProjection(
117                    String sql, String[] columnAliases, Type[] types) {
118    
119                    if (ArrayUtil.isEmpty(types)) {
120                            return new ProjectionImpl(
121                                    Projections.sqlProjection(sql, columnAliases, null));
122                    }
123    
124                    org.hibernate.type.Type[] hibernateTypes =
125                            new org.hibernate.type.Type[types.length];
126    
127                    for (int i = 0; i < types.length; i++) {
128                            hibernateTypes[i] = TypeTranslator.translate(types[i]);
129                    }
130    
131                    return new ProjectionImpl(
132                            Projections.sqlProjection(sql, columnAliases, hibernateTypes));
133            }
134    
135            @Override
136            public Projection sum(String propertyName) {
137                    return new ProjectionImpl(Projections.sum(propertyName));
138            }
139    
140    }