Search in sources :

Example 1 with PkHelper

use of com.manydesigns.portofino.util.PkHelper in project Portofino by ManyDesigns.

the class ManyToManyAction method jsonAssociations.

public Response jsonAssociations() throws JSONException {
    JSONObject response = new JSONObject();
    JSONArray enumList = new JSONArray();
    JSONObject model = new JSONObject();
    JSONObject titleMap = new JSONObject();
    JSONArray trueRelations = new JSONArray();
    JSONArray form = new JSONArray();
    JSONObject schema = new JSONObject();
    if (onePk != null) {
        try {
            loadAssociations();
            if (potentiallyAvailableAssociations == null) {
                return resourceActionNotConfigured();
            }
        } catch (NoSuchFieldException e) {
            return resourceActionNotConfigured();
        }
        booleanRelation = new LinkedHashMap<>();
        ClassAccessor ca = getManyTableAccessor();
        PkHelper pkHelper = new PkHelper(ca);
        for (Object obj : potentiallyAvailableAssociations) {
            String pk = StringUtils.join(pkHelper.generatePkStringArray(obj), "/");
            enumList.put(pk);
            titleMap.put(pk, ShortNameUtils.getName(ca, obj));
            if (!availableAssociations.contains(obj)) {
                trueRelations.put(pk);
            }
        }
        model.put(onePk.toString(), trueRelations);
        JSONObject items = new JSONObject();
        items.put("type", "string");
        items.put("enum", enumList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "array");
        jsonObject.put("title", "");
        jsonObject.put("items", items);
        JSONObject properties = new JSONObject();
        properties.put(onePk.toString(), jsonObject);
        schema.put("type", "object");
        schema.put("title", "Many to many");
        schema.put("properties", properties);
        JSONObject checkboxes = new JSONObject();
        checkboxes.put("key", onePk.toString());
        checkboxes.put("titleMap", titleMap);
        checkboxes.put("notitle", true);
        form.put(checkboxes);
    }
    if (!security.hasPermissions(getPortofinoConfiguration(), getActionInstance(), AccessLevel.VIEW, ManyToManyAction.PERMISSION_UPDATE)) {
        schema.put("readonly", true);
    }
    response.put("model", model);
    response.put("schema", schema);
    response.put("form", form);
    String jsonText = response.toString(2);
    return Response.ok(jsonText).type(MediaType.APPLICATION_JSON_TYPE).encoding("UTF-8").build();
}
Also used : JSONObject(org.json.JSONObject) JavaClassAccessor(com.manydesigns.elements.reflection.JavaClassAccessor) ClassAccessor(com.manydesigns.elements.reflection.ClassAccessor) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) PkHelper(com.manydesigns.portofino.util.PkHelper)

Aggregations

ClassAccessor (com.manydesigns.elements.reflection.ClassAccessor)1 JavaClassAccessor (com.manydesigns.elements.reflection.JavaClassAccessor)1 PkHelper (com.manydesigns.portofino.util.PkHelper)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1