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