001
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
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 }