package net.sourceforge.fenixedu.presentationTier.Action.externalServices; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Set; import org.apache.commons.lang.StringUtils; import org.json.JSONException; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import pt.ist.fenixWebFramework.FenixWebFramework; import pt.ist.fenixframework.pstm.AbstractDomainObject; import dml.DomainClass; import dml.DomainModel; import dml.Role; import dml.Slot; public class DomainObjectJSONSerializer { private static DomainModel model; static { model = FenixWebFramework.getDomainModel(); } public static JSONObject getDomainObject(AbstractDomainObject obj) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, JSONException { final JSONObject jsonObject = new JSONObject(); final Class clazz = obj.getClass(); final String objClassName = clazz.getName(); jsonObject.put("externalId", obj.getExternalId()); jsonObject.put("className", objClassName); final DomainClass domainClass = getDomainClass(objClassName); if (domainClass == null) { return jsonObject; } for (Slot slot : getAllSlots(domainClass)) { final String slotName = slot.getName(); final Method method = clazz.getMethod("get" + StringUtils.capitalize(slotName)); final Object result = method.invoke(obj); jsonObject.put(slotName, result == null ? null : result.toString()); } for (Role roleSlot : getAllRoleSlots(domainClass)) { final String slotName = roleSlot.getName(); if (roleSlot.getMultiplicityUpper() == 1) { final Method method = clazz.getMethod("get" + StringUtils.capitalize(slotName)); final AbstractDomainObject singleRelationObj = (AbstractDomainObject) method.invoke(obj); final JSONArray oneRelation = new JSONArray(); if (singleRelationObj != null) { oneRelation.add(singleRelationObj.getExternalId()); } jsonObject.put(slotName, oneRelation); } else { final Method method = clazz.getMethod("get" + StringUtils.capitalize(slotName) + "Set"); final Set result = (Set) method.invoke(obj); jsonObject.put(slotName, serializeRelation(result)); } } return jsonObject; } private static JSONArray serializeRelation(Set roleSet) { JSONArray jsonList = new JSONArray(); for (AbstractDomainObject obj : roleSet) { jsonList.add(obj.getExternalId()); } return jsonList; } private static DomainClass getDomainClass(String objClassName) { for (DomainClass clazz : model.getDomainClasses()) { if (clazz.getFullName().equals(objClassName)) { return clazz; } } return null; } private static List getAllSlots(DomainClass clazz) { final List slots = new ArrayList(); if (clazz.hasSuperclass()) { slots.addAll(getAllSlots((DomainClass) clazz.getSuperclass())); } slots.addAll(clazz.getSlotsList()); return slots; } private static List getAllRoleSlots(DomainClass clazz) { final List slots = new ArrayList(); if (clazz.hasSuperclass()) { slots.addAll(getAllRoleSlots((DomainClass) clazz.getSuperclass())); } slots.addAll(clazz.getRoleSlotsList()); return slots; } }