public static ReqRespHandler read(final Class<?> entityType) { return (ReqRespHandler) (req, resp) -> { Object id = Cls.convert(req.param("id"), idType(entityType)); return JPA.get(entityType, id); }; }
public static ReqRespHandler delete(final Class<?> entityType) { return (ReqRespHandler) (req, resp) -> { Object id = Cls.convert(req.param("id"), idType(entityType)); JPA.delete(entityType, id); return OK; }; }
public static ReqRespHandler update(final Class<?> entityType) { return (ReqRespHandler) (req, resp) -> { Object id = Cls.convert(req.param("id"), idType(entityType)); JPA.get(entityType, id); // make sure it exists JPA.merge(req.data(entityType)); // FIXME improve the merge return OK; }; }
public static ReqRespHandler edit(final Class<?> entityType, final String baseUri) { return (ReqRespHandler) (req, resp) -> { Object id = Cls.convert(req.param("id"), idType(entityType)); Object entity = JPA.getIfExists(entityType, id); if (entity == null) { return null; } JPA.detach(entity); if (resp.screen().title() == null) { resp.screen().title("Edit " + name(entityType)); } Btn save = btnSave(entity).go(baseUri + "/manage"); Btn cancel = GUI.btn("Cancel").go(uri(baseUri, entity) + "/view"); return GUI.edit(entity).buttons(save, cancel); }; }
public static String getLoginURL(Req req, OAuthProvider provider, String oauthDomain) { if (OAUTH.isEmpty()) { Log.warn("OAuth is not configured!"); } String name = provider.getName().toLowerCase(); Config providerConfig = OAUTH.sub(name); Value<String> clientId = providerConfig.entry("clientId").str(); Value<String> clientSecret = providerConfig.entry("clientSecret").str(); String callbackPath = Msc.specialUri(name + "OauthCallback"); boolean popup = req.param("popup", null) != null; String redirectUrl = U.notEmpty(oauthDomain) ? oauthDomain + callbackPath : HttpUtils.constructUrl(req, callbackPath); String statePrefix = popup ? "P" : "N"; String state = statePrefix + STATE_CHECK.generateState(clientSecret, req.sessionId()); try { OAuthClientRequest request = OAuthClientRequest.authorizationLocation(provider.getAuthEndpoint()) .setClientId(clientId.str().get()).setRedirectURI(redirectUrl).setScope(provider.getEmailScope()) .setState(state).setResponseType("code").buildQueryMessage(); return request.getLocationUri(); } catch (OAuthSystemException e) { throw U.rte(e); } }
public static ReqRespHandler view(final Class<?> entityType, final String baseUri) { final Class<?> idType = idType(entityType); return (ReqRespHandler) (req, resp) -> { final Object id = Cls.convert(req.param("id"), idType); final Object entity = JPA.getIfExists(entityType, id); if (entity == null) { return null; } JPA.detach(entity); String name = name(entityType); if (resp.screen().title() == null) { resp.screen().title(name + " Details"); } final Btn edit = GUI.btn("Edit").go(uri(baseUri, entity) + "/edit"); Btn all = GUI.btn("View all").go(baseUri + "/manage"); Btn del = btnDelete().go(baseUri + "/manage").onClick(() -> JPA.delete(entityType, id)).confirm("Do you really want to delete the " + name + "?"); Form form = GUI.show(entity).buttons(edit, all, del).visible(!del.clicked()); Object msg = del.clicked() ? GUI.div("The data was deleted.") : ""; return GUI.multi(form, msg); }; }
@Override public Object execute(Req req) throws Exception { String scope = req.param("scope"); return state.clients.get(scope); }
@Override public Object execute(Req req, Resp resp) throws Exception { Object id = Cls.convert(req.param("id"), idType(entityType)); return JPA.get(entityType, id); } };
@Override public Object execute(Req req, Resp resp) throws Exception { Object id = Cls.convert(req.param("id"), idType(entityType)); JPA.delete(entityType, id); return OK; } };
@Override public Object execute(Req req) throws Exception { String scope = req.param("scope"); PeerDiscoveryInfo info = new PeerDiscoveryInfo(req.clientIpAddress(), req.realIpAddress()); state.clients.get(scope).add(info); return NiceResponse.ok(req, "Successfully registered for discovery"); }
public Grid grid(final GuiConfig gui, Results items) { Req req = req(); Grid grid = GUI.grid(items); String q = req.param("find", null); if (q != null) grid.highlightRegex(Pattern.quote(q)); String highlight = req.param("$highlight", null); if (highlight != null) grid.highlightRegex(Pattern.quote(highlight)); String pageSize = req.param("$pageSize", null); if (pageSize != null) grid.pageSize(U.num(pageSize)); if (U.notEmpty(gui.uri())) { grid.toUri(new Mapper<Object, String>() { @Override public String map(Object item) throws Exception { return Render.template(gui.uri()).model(item); } }); } return grid; }
@Override public Object execute(Req req, Resp resp) throws Exception { Object id = Cls.convert(req.param("id"), idType(entityType)); JPA.get(entityType, id); // make sure it exists JPA.merge(req.data(entityType)); // FIXME improve the merge return OK; } };
@Override public Object execute(Req req, Resp resp) throws Exception { Object id = Cls.convert(req.param("id"), idType(entityType)); Object entity = JPA.getIfExists(entityType, id); if (entity == null) { return null; } JPA.detach(entity); if (resp.screen().title() == null) { resp.screen().title("Edit " + name(entityType)); } Btn save = btnSave(entity).go(baseUri + "/manage"); Btn cancel = GUI.btn("Cancel").go(uri(baseUri, entity) + "/view"); return GUI.edit(entity).buttons(save, cancel); } };
@Override public Object execute(Req req, final Resp resp) throws Exception { final Object id = Cls.convert(req.param("id"), idType); final Object entity = JPA.getIfExists(entityType, id); if (entity == null) { return null; } JPA.detach(entity); String name = name(entityType); if (resp.screen().title() == null) { resp.screen().title(name + " Details"); } final Btn edit = GUI.btn("Edit").go(uri(baseUri, entity) + "/edit"); Btn all = GUI.btn("View all").go(baseUri + "/manage"); Btn del = btnDelete().go(baseUri + "/manage").onClick(new Runnable() { @Override public void run() { JPA.delete(entityType, id); } }).confirm("Do you really want to delete the " + name + "?"); Form form = GUI.show(entity).buttons(edit, all, del).visible(!del.clicked()); Object msg = del.clicked() ? GUI.div("The data was deleted.") : ""; return GUI.multi(form, msg); } };