001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.petstore.console.pages;
018
019import org.apache.juneau.console.freemarker.*;
020import org.apache.juneau.console.views.*;
021import org.apache.juneau.console.views.RowAction.*;
022import org.apache.juneau.console.views.TableSpec.*;
023import org.apache.juneau.console.widgets.*;
024
025/**
026 * Builder twin of the Pending changes page (P10, {@code changes.ftlh}).
027 *
028 * <h5 class='section'>Example:</h5>
029 * <p class='bjava'>
030 *    <jk>return</jk> <jsm>twin</jsm>(<jv>req</jv>, ChangesPageBuilders.<jsm>changes</jsm>());
031 * </p>
032 */
033@SuppressWarnings({
034   "java:S1192" // Page-definition ids and labels repeat as inline literals by design.
035})
036public final class ChangesPageBuilders {
037
038   private ChangesPageBuilders() {}
039
040   /**
041    * P10 Pending changes.
042    *
043    * @return The page spec.
044    */
045   public static PageSpec changes() {
046      var selection = SelectionDef.create("id").selectAll(true).scope(SelectionDef.Scope.PERSISTENT).labelField("summary")
047         .selectableWhen(RowActionEnabledRule.of("state", Op.EQ, "PENDING", "Only pending changes can be discarded or applied."));
048      return PageSpec.create().tab("pets/changes").toolkit("views")
049         .table(TableSpec.create("changes").rowType("PendingChange").dataMode(DataMode.SERVER).dataUrl("/console/pets/changes/query")
050            .defaultOrder("id", Sort.DESC)
051            .columns(
052               Column.create("id").label("Change"),
053               Column.create("summary").label("Change summary"),
054               Column.create("author").label("Author"),
055               Column.create("state").label("State").render("pill"),
056               Column.create("message").label("Message").defaultVisible(false))
057            .ribbon(
058               RibbonItem.option("pending-only").title("Pending only").column("state").value("$eq(PENDING)").defaultOn(),
059               RibbonItem.refresh())
060            .rowActions(
061               RowAction.create("retry").label("Retry").endpoint("/console/pets/changes/{id}/retry").method(Method.POST).onSuccess(OnSuccess.REDRAW)
062                  .visibleWhen(VisibilityRule.when("state").eq("FAILED")))
063            .selection(selection)
064            .bulk(BulkMutateDef.create(WritePermit.forCapability("pets.changes"), selection).actions(
065               RowAction.create("discard").label("Discard").endpoint("/console/pets/changes/discard").method(Method.POST).bulkMode(BulkMode.AGGREGATE)
066                  .confirmTitle("Discard changes").confirm("Discard the selected changes?").tone(Tone.DANGER).confirmRenderer("typed"),
067               RowAction.create("apply").label("Apply").endpoint("/console/pets/changes/{id}/apply").method(Method.POST).bulkMode(BulkMode.PER_ROW))),
068            "Pending changes");
069   }
070}