Search in sources :

Example 1 with MCRNavigation

use of org.mycore.wcms2.datamodel.MCRNavigation in project mycore by MyCoRe-Org.

the class MCRWCMSNavigationManager method updateHref.

/**
 * Runs recursive through the item tree and changes each href and hrefStartingPage attribute to the new href.
 *
 * @param item
 *            navigation item to change (and all its children)
 * @param from
 *            which href to change
 * @param to
 *            new value of href
 * @return if something in the tree was changed
 */
public static boolean updateHref(MCRNavigationBaseItem item, String from, String to) {
    boolean dirty = false;
    if (item instanceof MCRNavigation) {
        MCRNavigation navigation = (MCRNavigation) item;
        if (navigation.getHrefStartingPage() != null && navigation.getHrefStartingPage().equals(from)) {
            navigation.setHrefStartingPage(to);
            dirty = true;
        }
    } else if (item instanceof MCRNavigationItem) {
        MCRNavigationItem navItem = (MCRNavigationItem) item;
        if (navItem.getHref().equals(from)) {
            navItem.setHref(to);
            dirty = true;
        }
    }
    if (item instanceof MCRNavigationItemContainer) {
        MCRNavigationItemContainer container = (MCRNavigationItemContainer) item;
        for (MCRNavigationBaseItem child : container.getChildren()) {
            if (updateHref(child, from, to)) {
                dirty = true;
            }
        }
    }
    return dirty;
}
Also used : MCRNavigation(org.mycore.wcms2.datamodel.MCRNavigation) MCRNavigationItem(org.mycore.wcms2.datamodel.MCRNavigationItem) MCRNavigationItemContainer(org.mycore.wcms2.datamodel.MCRNavigationItemContainer) MCRNavigationBaseItem(org.mycore.wcms2.datamodel.MCRNavigationBaseItem)

Example 2 with MCRNavigation

use of org.mycore.wcms2.datamodel.MCRNavigation in project mycore by MyCoRe-Org.

the class MCRWCMSNavigationResource method save.

@POST
@Path("save")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response save(String json) throws Exception {
    JsonStreamParser jsonStreamParser = new JsonStreamParser(json);
    if (!jsonStreamParser.hasNext()) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    JsonObject saveObject = jsonStreamParser.next().getAsJsonObject();
    // get navigation
    MCRNavigation newNavigation = MCRWCMSNavigationManager.fromJSON(saveObject);
    // save navigation
    MCRWCMSNavigationManager.save(newNavigation);
    // save content
    JsonArray items = saveObject.get(MCRWCMSNavigationProvider.JSON_ITEMS).getAsJsonArray();
    getContentManager().save(items);
    return Response.ok().build();
}
Also used : MCRNavigation(org.mycore.wcms2.datamodel.MCRNavigation) JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) JsonStreamParser(com.google.gson.JsonStreamParser) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 3 with MCRNavigation

use of org.mycore.wcms2.datamodel.MCRNavigation in project mycore by MyCoRe-Org.

the class MCRWCMSDefaultNavigationProvider method add.

private JsonObject add(MCRNavigationBaseItem item, JsonArray hierarchy, JsonArray items) {
    int id = items.size();
    JsonObject jsonItem = gson.toJsonTree(item).getAsJsonObject();
    jsonItem.addProperty(JSON_WCMS_ID, id);
    jsonItem.remove(JSON_CHILDREN);
    WCMSType type = null;
    String href = null;
    if (item instanceof MCRNavigation) {
        type = WCMSType.root;
        href = ((MCRNavigation) item).getHrefStartingPage();
    } else if (item instanceof MCRNavigationMenuItem) {
        type = WCMSType.menu;
        href = ((MCRNavigationMenuItem) item).getDir();
    } else if (item instanceof MCRNavigationItem) {
        type = WCMSType.item;
        href = ((MCRNavigationItem) item).getHref();
    } else if (item instanceof MCRNavigationInsertItem) {
        type = WCMSType.insert;
    } else if (item instanceof MCRNavigationGroup) {
        type = WCMSType.group;
    } else {
        LOGGER.warn("Unable to set type for item {}", id);
    }
    jsonItem.addProperty(JSON_WCMS_TYPE, type.name());
    if (href != null) {
        jsonItem.add("access", getAccess(href));
    }
    items.add(jsonItem);
    // create hierarchy for root
    JsonObject hierarchyObject = new JsonObject();
    hierarchyObject.addProperty(JSON_WCMS_ID, id);
    hierarchy.add(hierarchyObject);
    return hierarchyObject;
}
Also used : MCRNavigation(org.mycore.wcms2.datamodel.MCRNavigation) MCRNavigationItem(org.mycore.wcms2.datamodel.MCRNavigationItem) MCRNavigationMenuItem(org.mycore.wcms2.datamodel.MCRNavigationMenuItem) JsonObject(com.google.gson.JsonObject) MCRNavigationInsertItem(org.mycore.wcms2.datamodel.MCRNavigationInsertItem) MCRNavigationGroup(org.mycore.wcms2.datamodel.MCRNavigationGroup)

Example 4 with MCRNavigation

use of org.mycore.wcms2.datamodel.MCRNavigation in project mycore by MyCoRe-Org.

the class MCRWCMSDefaultNavigationProvider method fromJSON.

@Override
public MCRNavigation fromJSON(JsonObject navigationJSON) {
    JsonArray items = navigationJSON.get(JSON_ITEMS).getAsJsonArray();
    JsonArray hierarchy = navigationJSON.get(JSON_HIERARCHY).getAsJsonArray();
    if (hierarchy.size() > 0) {
        JsonObject root = hierarchy.get(0).getAsJsonObject();
        MCRNavigationBaseItem navigation = createItem(root, items);
        if (navigation instanceof MCRNavigation) {
            return (MCRNavigation) navigation;
        }
    }
    return null;
}
Also used : JsonArray(com.google.gson.JsonArray) MCRNavigation(org.mycore.wcms2.datamodel.MCRNavigation) MCRNavigationBaseItem(org.mycore.wcms2.datamodel.MCRNavigationBaseItem) JsonObject(com.google.gson.JsonObject)

Example 5 with MCRNavigation

use of org.mycore.wcms2.datamodel.MCRNavigation in project mycore by MyCoRe-Org.

the class MCRWCMSNavigationResource method move.

@POST
@Path("move")
public void move(@QueryParam("from") String from, @QueryParam("to") String to) throws Exception {
    if (from == null || to == null) {
        throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("from or to parameter not set").build());
    }
    // move it
    getContentManager().move(from, to);
    // update navigation
    MCRNavigation navigation = MCRWCMSNavigationManager.getNavigation();
    boolean hrefUpdated = MCRWCMSNavigationManager.updateHref(navigation, from, to);
    if (hrefUpdated) {
        MCRWCMSNavigationManager.save(navigation);
    }
}
Also used : MCRNavigation(org.mycore.wcms2.datamodel.MCRNavigation) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

MCRNavigation (org.mycore.wcms2.datamodel.MCRNavigation)7 JsonObject (com.google.gson.JsonObject)3 JsonArray (com.google.gson.JsonArray)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 JAXBContext (javax.xml.bind.JAXBContext)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 MCRNavigationBaseItem (org.mycore.wcms2.datamodel.MCRNavigationBaseItem)2 MCRNavigationItem (org.mycore.wcms2.datamodel.MCRNavigationItem)2 JsonStreamParser (com.google.gson.JsonStreamParser)1 Consumes (javax.ws.rs.Consumes)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 MCRNavigationGroup (org.mycore.wcms2.datamodel.MCRNavigationGroup)1 MCRNavigationInsertItem (org.mycore.wcms2.datamodel.MCRNavigationInsertItem)1 MCRNavigationItemContainer (org.mycore.wcms2.datamodel.MCRNavigationItemContainer)1 MCRNavigationMenuItem (org.mycore.wcms2.datamodel.MCRNavigationMenuItem)1