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.util.bridges.alloy;
016    
017    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.Property;
019    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.util.TextFormatter;
022    import com.liferay.portal.model.BaseModel;
023    
024    import java.lang.reflect.Method;
025    
026    import java.util.List;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class AlloyServiceInvoker {
032    
033            public AlloyServiceInvoker(String className) {
034                    Class<?> clazz = getClass();
035    
036                    ClassLoader classLoader = clazz.getClassLoader();
037    
038                    int pos = className.indexOf(".model.");
039    
040                    String simpleClassName = className.substring(pos + 7);
041    
042                    String serviceClassName =
043                            className.substring(0, pos) + ".service." + simpleClassName +
044                                    "LocalServiceUtil";
045    
046                    try {
047                            Class<?> serviceClass = classLoader.loadClass(serviceClassName);
048    
049                            dynamicQueryCountMethod = serviceClass.getMethod(
050                                    "dynamicQueryCount", new Class[] {DynamicQuery.class});
051                            dynamicQueryMethod1 = serviceClass.getMethod(
052                                    "dynamicQuery", new Class[0]);
053                            dynamicQueryMethod2 = serviceClass.getMethod(
054                                    "dynamicQuery", new Class[] {DynamicQuery.class});
055                            dynamicQueryMethod3 = serviceClass.getMethod(
056                                    "dynamicQuery",
057                                    new Class[] {DynamicQuery.class, int.class, int.class});
058                            dynamicQueryMethod4 = serviceClass.getMethod(
059                                    "dynamicQuery",
060                                    new Class[] {DynamicQuery.class, int.class, int.class,
061                                            OrderByComparator.class});
062                            fetchModelMethod = serviceClass.getMethod(
063                                    "fetch" + simpleClassName, new Class[] {long.class});
064                            getModelsCountMethod = serviceClass.getMethod(
065                                    "get" + TextFormatter.formatPlural(simpleClassName) + "Count",
066                                    new Class[0]);
067                            getModelsMethod = serviceClass.getMethod(
068                                    "get" + TextFormatter.formatPlural(simpleClassName),
069                                    new Class[] {int.class, int.class});
070                    }
071                    catch (Exception e) {
072                            throw new RuntimeException(e);
073                    }
074            }
075    
076            public DynamicQuery buildDynamicQuery() throws Exception {
077                    return (DynamicQuery)dynamicQueryMethod1.invoke(false);
078            }
079    
080            public DynamicQuery buildDynamicQuery(Object[] properties)
081                    throws Exception {
082    
083                    if ((properties.length == 0) || ((properties.length % 2) != 0)) {
084                            throw new IllegalArgumentException(
085                                    "Properties length is not an even number");
086                    }
087    
088                    DynamicQuery dynamicQuery = buildDynamicQuery();
089    
090                    for (int i = 0; i < properties.length; i += 2) {
091                            String propertyName = String.valueOf(properties[i]);
092    
093                            Property property = PropertyFactoryUtil.forName(propertyName);
094    
095                            Object propertyValue = (properties[i + 1]);
096    
097                            dynamicQuery.add(property.eq(propertyValue));
098                    }
099    
100                    return dynamicQuery;
101            }
102    
103            /**
104             * @deprecated As of 6.2.0, replaced by {@link
105             *             #executeDynamicQuery(DynamicQuery)}
106             */
107            @SuppressWarnings("rawtypes")
108            public List dynamicQuery(DynamicQuery dynamicQuery) throws Exception {
109                    return executeDynamicQuery(dynamicQuery);
110            }
111    
112            /**
113             * @deprecated As of 6.2.0, replaced by {@link
114             *             #executeDynamicQueryCount(DynamicQuery)}
115             */
116            public long dynamicQueryCount(DynamicQuery dynamicQuery) throws Exception {
117                    return executeDynamicQueryCount(dynamicQuery);
118            }
119    
120            @SuppressWarnings("rawtypes")
121            public List executeDynamicQuery(DynamicQuery dynamicQuery)
122                    throws Exception {
123    
124                    return (List)dynamicQueryMethod2.invoke(false, dynamicQuery);
125            }
126    
127            @SuppressWarnings("rawtypes")
128            public List executeDynamicQuery(
129                            DynamicQuery dynamicQuery, int start, int end)
130                    throws Exception {
131    
132                    return (List)dynamicQueryMethod3.invoke(
133                            false, dynamicQuery, start, end);
134            }
135    
136            @SuppressWarnings("rawtypes")
137            public List executeDynamicQuery(
138                            DynamicQuery dynamicQuery, int start, int end,
139                            OrderByComparator obc)
140                    throws Exception {
141    
142                    return (List)dynamicQueryMethod4.invoke(
143                            false, dynamicQuery, start, end, obc);
144            }
145    
146            @SuppressWarnings("rawtypes")
147            public List executeDynamicQuery(Object[] properties) throws Exception {
148                    return executeDynamicQuery(buildDynamicQuery(properties));
149            }
150    
151            @SuppressWarnings("rawtypes")
152            public List executeDynamicQuery(Object[] properties, int start, int end)
153                    throws Exception {
154    
155                    return executeDynamicQuery(buildDynamicQuery(properties), start, end);
156            }
157    
158            @SuppressWarnings("rawtypes")
159            public List executeDynamicQuery(
160                            Object[] properties, int start, int end, OrderByComparator obc)
161                    throws Exception {
162    
163                    return executeDynamicQuery(
164                            buildDynamicQuery(properties), start, end, obc);
165            }
166    
167            public long executeDynamicQueryCount(DynamicQuery dynamicQuery)
168                    throws Exception {
169    
170                    return (Long)dynamicQueryCountMethod.invoke(false, dynamicQuery);
171            }
172    
173            public long executeDynamicQueryCount(Object[] properties) throws Exception {
174                    return executeDynamicQueryCount(buildDynamicQuery(properties));
175            }
176    
177            public BaseModel<?> fetchModel(long classPK) throws Exception {
178                    return (BaseModel<?>)fetchModelMethod.invoke(false, classPK);
179            }
180    
181            @SuppressWarnings("rawtypes")
182            public List getModels(int start, int end) throws Exception {
183                    return (List)getModelsMethod.invoke(false, start, end);
184            }
185    
186            public int getModelsCount() throws Exception {
187                    return (Integer)getModelsCountMethod.invoke(false);
188            }
189    
190            protected Method dynamicQueryCountMethod;
191            protected Method dynamicQueryMethod1;
192            protected Method dynamicQueryMethod2;
193            protected Method dynamicQueryMethod3;
194            protected Method dynamicQueryMethod4;
195            protected Method fetchModelMethod;
196            protected Method getModelsCountMethod;
197            protected Method getModelsMethod;
198    
199    }