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.commons.beanquery.*;
020import org.apache.juneau.console.freemarker.*;
021import org.apache.juneau.console.views.*;
022import org.apache.juneau.console.views.RowAction.*;
023import org.apache.juneau.console.views.TableSpec.*;
024import org.apache.juneau.console.widgets.*;
025
026/**
027 * Builder twin of the Pending changes page (P10, {@code changes.ftlh}).
028 *
029 * <h2 class='section'>Example:</h2>
030 * <p class='bjava'>
031 *    <jk>return</jk> <jsm>twin</jsm>(<jv>req</jv>, ChangesPageBuilders.<jsm>changes</jsm>());
032 * </p>
033 */
034@SuppressWarnings({
035   "java:S1192" // Page-definition ids and labels repeat as inline literals by design.
036})
037public final class ChangesPageBuilders {
038
039   private ChangesPageBuilders() {}
040
041   /**
042    * P10 Pending changes.
043    *
044    * @return The page spec.
045    */
046   public static PageSpec changes() {
047      var selection = SelectionDef.create("id").selectAll(true).scope(SelectionDef.Scope.PERSISTENT).labelField("summary")
048         .selectableWhen(RowActionEnabledRule.of("state", Op.EQ, "PENDING", "Only pending changes can be discarded or applied."));
049      return PageSpec.create().tab("pets/changes").toolkit("views", "datatables-buttons")
050         .table(TableSpec.create("changes").rowType("PendingChange").dataMode(DataMode.SERVER).dataUrl("/console/pets/changes/query")
051            .operators(SearchOperatorSet.standard())
052            .defaultOrder("id", Sort.DESC)
053            .detail(RowDetails.change())
054            .columns(
055               Column.create("id").label("Change").searchType(SearchType.NUMERIC),
056               Column.create("summary").label("Change summary").searchType(SearchType.TEXT),
057               Column.create("author").label("Author").searchType(SearchType.TEXT),
058               Column.create("state").label("State").render(PillRenders.changeState()).searchType(SearchType.ENUM),
059               Column.create("message").label("Message").searchType(SearchType.TEXT).defaultVisible(false))
060            .columnConfig(true)
061            .ribbon(Ribbons.standard()
062               .filter(RibbonItem.option("pending-only").title("Pending only").symbol("hourglass").column("state").value("$eq(PENDING)").defaultOn())
063               .build())
064            .rowActions(
065               RowAction.create("retry").label("Retry").endpoint("/console/pets/changes/{id}/retry").method(Method.POST).onSuccess(OnSuccess.REDRAW)
066                  .visibleWhen(VisibilityRule.when("state").eq("FAILED")))
067            .selection(selection)
068            .bulk(BulkMutateDef.create(WritePermit.forCapability("pets.changes"), selection).actions(
069               RowAction.create("discard").label("Discard").endpoint("/console/pets/changes/discard").method(Method.POST).bulkMode(BulkMode.AGGREGATE)
070                  .confirmTitle("Discard changes").confirm("Discard the selected changes?").tone(Tone.DANGER).confirmRenderer("typed"),
071               RowAction.create("apply").label("Apply").endpoint("/console/pets/changes/{id}/apply").method(Method.POST).bulkMode(BulkMode.PER_ROW))),
072            "Pending changes");
073   }
074}