View Javadoc
1   package fr.ifremer.reefdb.service;
2   
3   /*
4    * #%L
5    * Reef DB :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.ImmutableList;
27  import fr.ifremer.quadrige3.core.dao.referential.StatusCode;
28  import fr.ifremer.quadrige3.ui.core.dto.referential.StatusDTO;
29  
30  import java.util.List;
31  
32  /**
33   * Enumeration to provide status codes from national or local status
34   *
35   * @author Ludovic
36   */
37  public enum StatusFilter {
38  
39      NATIONAL(ImmutableList.of(StatusCode.ENABLE.getValue(), StatusCode.DISABLE.getValue(), StatusCode.TEMPORARY.getValue())),
40      NATIONAL_ACTIVE(ImmutableList.of(StatusCode.ENABLE.getValue())),
41      LOCAL(ImmutableList.of(StatusCode.LOCAL_ENABLE.getValue(), StatusCode.LOCAL_DISABLE.getValue())),
42      LOCAL_ACTIVE(ImmutableList.of(StatusCode.LOCAL_ENABLE.getValue())),
43      ACTIVE(ImmutableList.of(StatusCode.ENABLE.getValue(), StatusCode.LOCAL_ENABLE.getValue())),
44      ALL(ImmutableList.of(StatusCode.ENABLE.getValue(), StatusCode.DISABLE.getValue(), StatusCode.TEMPORARY.getValue(),
45              StatusCode.LOCAL_ENABLE.getValue(), StatusCode.LOCAL_DISABLE.getValue()));
46  
47      private final List<String> statusCodes;
48  
49      StatusFilter(List<String> statusCodes) {
50          this.statusCodes = statusCodes;
51      }
52  
53      /**
54       * <p>toStatusCodes.</p>
55       *
56       * @return a {@link java.util.List} object.
57       */
58      public List<String> toStatusCodes() {
59          return statusCodes;
60      }
61  
62      /**
63       * <p>intersect.</p>
64       *
65       * @param statusCode a {@link java.lang.String} object.
66       * @return a {@link java.util.List} object.
67       */
68      public List<String> intersect(String statusCode) {
69          if (statusCode == null) {
70              return statusCodes;
71          }
72          if (statusCodes.contains(statusCode)) {
73              return ImmutableList.of(statusCode);
74          } else {
75              return ImmutableList.of("BAD");
76          }
77      }
78  
79      /**
80       * <p>intersect.</p>
81       *
82       * @param status a {@link StatusDTO} object.
83       * @return a {@link java.util.List} object.
84       */
85      public List<String> intersect(StatusDTO status) {
86          if (status == null) {
87              return statusCodes;
88          } else {
89              return intersect(status.getCode());
90          }
91      }
92  
93      /**
94       * <p>toLocalOrNational.</p>
95       *
96       * @param isLocal a {@link java.lang.Boolean} object.
97       * @return a {@link fr.ifremer.reefdb.service.StatusFilter} object.
98       */
99      public static StatusFilter toLocalOrNational(Boolean isLocal) {
100         if (isLocal == null) {
101             return ALL;
102         } else {
103             return isLocal ? LOCAL : NATIONAL;
104         }
105     }
106 
107     /**
108      * <p>toActiveLocalOrNational.</p>
109      *
110      * @param isLocal a {@link java.lang.Boolean} object.
111      * @return a {@link fr.ifremer.reefdb.service.StatusFilter} object.
112      */
113     public static StatusFilter toActiveLocalOrNational(Boolean isLocal) {
114         if (isLocal == null) {
115             return ACTIVE;
116         } else {
117             return isLocal ? LOCAL_ACTIVE : NATIONAL_ACTIVE;
118         }
119     }
120 
121 }