View Javadoc
1   /*
2    * Copyright (c) 2002-2024, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common;
35  
36  import com.fasterxml.jackson.annotation.JsonCreator;
37  import com.fasterxml.jackson.annotation.JsonFormat;
38  import com.fasterxml.jackson.annotation.JsonIgnore;
39  import com.fasterxml.jackson.annotation.JsonInclude;
40  import com.fasterxml.jackson.annotation.JsonProperty;
41  
42  import java.util.Arrays;
43  import java.util.List;
44  import java.util.Objects;
45  import java.util.stream.Collectors;
46  
47  import static fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.AttributeChangeStatusType.ERROR;
48  import static fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.AttributeChangeStatusType.SUCCESS;
49  import static fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.AttributeChangeStatusType.WARNING;
50  
51  @JsonInclude( JsonInclude.Include.NON_NULL )
52  @JsonFormat( shape = JsonFormat.Shape.OBJECT )
53  public enum AttributeChangeStatus
54  {
55      CREATED( "created", null, SUCCESS ),
56      NOT_CREATED( "not_created", "This attribute was not created because of an invalid provided value.", ERROR ),
57      UPDATED( "updated", null, SUCCESS ),
58      REMOVED( "removed", null, SUCCESS ),
59      NOT_REMOVED( "not_removed", "This mandatory attribute cannot be removed.", ERROR ),
60      NOT_UPDATED( "not_updated", "This attribute already exists with the same value and certificate.", WARNING ),
61      NOT_FOUND( "not_found", "This attribute does not exist in the repository.", ERROR ),
62      INSUFFICIENT_CERTIFICATION_LEVEL( "insufficient_certification_level",
63              "This attribute cannot be updated because the existing certification level is higher than the level of the process provided in the request.",
64              ERROR ),
65      INSUFFICIENT_RIGHTS( "insufficient_rights",
66              "This attribute cannot be written because the associated process in the request does not match the contract definition.", ERROR ),
67      UNAUTHORIZED( "unauthorized", "This attribute is not writable in service contract definition", ERROR ),
68      UNKNOWN_GEOCODES_CODE( "unknown_geocodes_code", "The provided code was not found in the Geocodes repository.", ERROR ),
69      UNKNOWN_GEOCODES_LABEL( "unknown_geocodes_label", "The provided label was not found in the Geocodes repository.", ERROR ),
70      MULTIPLE_GEOCODES_RESULTS_FOR_LABEL( "multiple_geocodes_results_for_label",
71              "The provided label correspond to multiple results in Geocodes. Please specify by providing the code.", ERROR ),
72      OVERRIDDEN_GEOCODES_LABEL( "overridden_geocodes_label",
73              "The provided label was not corresponding to the provided code, and has been overridden with the correct Geocodes label.", SUCCESS ),
74      INVALID_VALUE( "invalid_value", "This attribute value doesn't match its validation pattern.", ERROR ),
75      FORMATTED_VALUE( "formatted_value", "This attribute value has been formatted before treatment.", WARNING ),
76      UNCERTIFIED( "uncertified", null, SUCCESS );
77  
78      @JsonIgnore
79      private static final List<AttributeChangeStatus> SUCCESS_STATUSES = Arrays.stream( values( ) ).filter( s -> s.type == SUCCESS )
80              .collect( Collectors.toList( ) );
81  
82      @JsonProperty( "code" )
83      private String code;
84  
85      @JsonProperty( "message" )
86      private String message;
87  
88      @JsonProperty( "type" )
89      private AttributeChangeStatusType type;
90  
91      AttributeChangeStatus( final String status, final String message, final AttributeChangeStatusType type )
92      {
93          this.code = status;
94          this.message = message;
95          this.type = type;
96      }
97  
98      @JsonCreator
99      public static AttributeChangeStatus forValues( @JsonProperty( "code" ) String code, @JsonProperty( "message" ) String message )
100     {
101         return Arrays.stream( AttributeChangeStatus.values( ) ).filter( attributeChangeStatus -> Objects.equals( code, attributeChangeStatus.getCode( ) )
102                 && Objects.equals( message, attributeChangeStatus.getMessage( ) ) ).findFirst( ).orElse( null );
103     }
104 
105     public String getCode( )
106     {
107         return code;
108     }
109 
110     public void setCode( String code )
111     {
112         this.code = code;
113     }
114 
115     public String getMessage( )
116     {
117         return message;
118     }
119 
120     public void setMessage( String message )
121     {
122         this.message = message;
123     }
124 
125     public AttributeChangeStatusType getType( )
126     {
127         return type;
128     }
129 
130     public void setType( final AttributeChangeStatusType type )
131     {
132         this.type = type;
133     }
134 
135     @JsonIgnore
136     public static List<AttributeChangeStatus> getSuccessStatuses( )
137     {
138         return SUCCESS_STATUSES;
139     }
140 
141 }