View Javadoc
1   /*
2    * Prometheus: Application Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.prometheus.validate;
18  
19  import io.github.tonywasher.joceanus.prometheus.data.PrometheusData.PrometheusDataItemCtl;
20  import io.github.tonywasher.joceanus.prometheus.data.PrometheusData.PrometheusDataValidator;
21  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
22  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataResource;
23  import io.github.tonywasher.joceanus.prometheus.data.PrometheusStaticDataItem;
24  import io.github.tonywasher.joceanus.prometheus.data.PrometheusStaticDataItem.PrometheusStaticDataMap;
25  import io.github.tonywasher.joceanus.prometheus.data.PrometheusStaticDataItem.PrometheusStaticList;
26  
27  /**
28   * Validator for static.
29   */
30  public class PrometheusValidateStatic
31          implements PrometheusDataValidator {
32      /**
33       * Default constructor.
34       */
35      public PrometheusValidateStatic() {
36          /* NoOp */
37      }
38  
39      @Override
40      public void validate(final PrometheusDataItemCtl pStatic) {
41          final PrometheusStaticDataItem myStatic = (PrometheusStaticDataItem) pStatic;
42          final PrometheusStaticList<?> myList = myStatic.getList();
43          final String myName = myStatic.getName();
44          final String myDesc = myStatic.getDesc();
45          final PrometheusStaticDataMap<?> myMap = myList.getDataMap();
46  
47          /* Name must be non-null */
48          if (myName == null) {
49              myStatic.addError(PrometheusDataItem.ERROR_MISSING, PrometheusDataResource.DATAITEM_FIELD_NAME);
50  
51              /* Else check the name */
52          } else {
53              /* The name must not be too long */
54              if (PrometheusDataItem.byteLength(myName) > PrometheusDataItem.NAMELEN) {
55                  myStatic.addError(PrometheusDataItem.ERROR_LENGTH, PrometheusDataResource.DATAITEM_FIELD_NAME);
56              }
57  
58              /* The name must only contain valid characters */
59              if (!PrometheusDataItem.validString(myName, null)) {
60                  myStatic.addError(PrometheusStaticDataItem.ERROR_BADNAME, PrometheusDataResource.DATAITEM_FIELD_NAME);
61              }
62  
63              /* Check that the name is unique */
64              if (!myMap.validNameCount(myName)) {
65                  myStatic.addError(PrometheusDataItem.ERROR_DUPLICATE, PrometheusDataResource.DATAITEM_FIELD_NAME);
66              }
67          }
68  
69          /* Check description length */
70          if (myDesc != null
71                  && PrometheusDataItem.byteLength(myDesc) > PrometheusDataItem.DESCLEN) {
72              myStatic.addError(PrometheusDataItem.ERROR_LENGTH, PrometheusDataResource.DATAITEM_FIELD_DESC);
73          }
74  
75          /* The order must not be negative */
76          if (myStatic.getOrder() < 0) {
77              myStatic.addError(PrometheusDataItem.ERROR_NEGATIVE, PrometheusDataResource.STATICDATA_SORT);
78          }
79  
80          /* Cannot have duplicate order */
81          if (!myMap.validOrderCount(myStatic.getOrder())) {
82              myStatic.addError(PrometheusDataItem.ERROR_DUPLICATE, PrometheusDataResource.STATICDATA_SORT);
83          }
84  
85          /* Set validation flag */
86          if (!myStatic.hasErrors()) {
87              myStatic.setValidEdit();
88          }
89      }
90  }