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.console.freemarker;
018
019import java.util.*;
020
021/**
022 * A rail section holding one ordinary card.
023 *
024 * <p>
025 * The card publishes what it publishes; the section owns no topic.  The card is added to the page by
026 * {@code PageSpec.dashboard(...)}.
027 * </p>
028 *
029 * <h5 class='section'>Example:</h5>
030 * <p class='bjava'>
031 *    CardSection.<jsm>of</jsm>(ChartDef.<jsm>donut</jsm>(<js>"share"</js>).src(<js>"/stats/share"</js>).label(<js>"reason"</js>).value(<js>"count"</js>));
032 * </p>
033 *
034 * @since 10.0.0
035 */
036public final class CardSection extends Section {
037
038   private final CardSpec card;
039   private boolean published;
040
041   private CardSection(CardSpec card) {
042      this.card = card;
043   }
044
045   /**
046    * Creates a card section from a chart.
047    *
048    * @param chart The chart.
049    * @return A new section.
050    */
051   public static CardSection of(ChartDef chart) {
052      return new CardSection(Objects.requireNonNull(chart, "chart must not be null").toCardSpec());
053   }
054
055   /**
056    * Creates a card section from any card.
057    *
058    * @param card The card.  It must not be bare.
059    * @return A new section.
060    */
061   public static CardSection of(CardSpec card) {
062      return new CardSection(Objects.requireNonNull(card, "card must not be null"));
063   }
064
065   /**
066    * Not allowed: a card section publishes through its card.  Calling this makes the dashboard fail with E-77.
067    *
068    * @param topic Ignored.
069    * @return This object.
070    */
071   @Override /* Section */
072   public CardSection publishes(String topic) {
073      published = true;
074      return this;
075   }
076
077   @Override /* Section */
078   public CardSection label(String label) {
079      super.label(label);
080      return this;
081   }
082
083   @Override String kind() { return "card"; }
084
085   @Override String id() {
086      return null;
087   }
088
089   @Override List<CardSpec> cards() {
090      return List.of(card);
091   }
092
093   @Override void check(String dash) {
094      if (published)
095         throw e77(dash, "card section '" + card.id() + "' publishes through its card; remove publishes(...)");
096      checkOwnedCard(dash, card);
097   }
098
099   @Override Map<String,Object> toMap() {
100      var m = new LinkedHashMap<String,Object>();
101      m.put("kind", "card");
102      m.put("card", card.id());
103      if (label != null)
104         m.put("label", label);
105      return m;
106   }
107
108   @Override void body(Map<String,Object> m) {
109      // Unused: toMap is overridden.
110   }
111}