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.util.TextFormatter;
019    import com.liferay.portal.model.BaseModel;
020    
021    import java.lang.reflect.Method;
022    
023    import java.util.List;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class AlloyServiceInvoker {
029    
030            public AlloyServiceInvoker(String className) {
031                    Class<?> clazz = getClass();
032    
033                    ClassLoader classLoader = clazz.getClassLoader();
034    
035                    int pos = className.indexOf(".model.");
036    
037                    String simpleClassName = className.substring(pos + 7);
038    
039                    String serviceClassName =
040                            className.substring(0, pos) + ".service." + simpleClassName +
041                                    "LocalServiceUtil";
042    
043                    try {
044                            Class<?> serviceClass = classLoader.loadClass(serviceClassName);
045    
046                            dynamicQueryCountMethod = serviceClass.getMethod(
047                                    "dynamicQueryCount", new Class[] {DynamicQuery.class});
048                            dynamicQueryMethod = serviceClass.getMethod(
049                                    "dynamicQuery", new Class[] {DynamicQuery.class});
050                            fetchModelMethod = serviceClass.getMethod(
051                                    "fetch" + simpleClassName, new Class[] {long.class});
052                            getModelsCountMethod = serviceClass.getMethod(
053                                    "get" + TextFormatter.formatPlural(simpleClassName) + "Count",
054                                    new Class[0]);
055                            getModelsMethod = serviceClass.getMethod(
056                                    "get" + TextFormatter.formatPlural(simpleClassName),
057                                    new Class[] {int.class, int.class});
058                    }
059                    catch (Exception e) {
060                            throw new RuntimeException(e);
061                    }
062            }
063    
064            @SuppressWarnings("rawtypes")
065            public List dynamicQuery(DynamicQuery dynamicQuery) throws Exception {
066                    return (List)dynamicQueryMethod.invoke(false, dynamicQuery);
067            }
068    
069            public long dynamicQueryCount(DynamicQuery dynamicQuery) throws Exception {
070                    return (Long)dynamicQueryCountMethod.invoke(false, dynamicQuery);
071            }
072    
073            public BaseModel<?> fetchModel(long classPK) throws Exception {
074                    return (BaseModel<?>)fetchModelMethod.invoke(false, classPK);
075            }
076    
077            @SuppressWarnings("rawtypes")
078            public List getModels(int start, int end) throws Exception {
079                    return (List)getModelsMethod.invoke(false, start, end);
080            }
081    
082            public int getModelsCount() throws Exception {
083                    return (Integer)getModelsCountMethod.invoke(false);
084            }
085    
086            protected Method dynamicQueryCountMethod;
087            protected Method dynamicQueryMethod;
088            protected Method fetchModelMethod;
089            protected Method getModelsCountMethod;
090            protected Method getModelsMethod;
091    
092    }