View Javadoc
1   package fr.ifremer.dali.dto.enums;
2   
3   /*
4    * #%L
5    * Dali :: 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  
27  import fr.ifremer.quadrige3.core.dao.system.synchronization.SynchronizationStatus;
28  import fr.ifremer.dali.dto.DaliBeanFactory;
29  import fr.ifremer.dali.dto.SynchronizationStatusDTO;
30  import org.apache.commons.lang3.StringUtils;
31  
32  import static org.nuiton.i18n.I18n.n;
33  import static org.nuiton.i18n.I18n.t;
34  
35  /**
36   * Partage enum.
37   */
38  public enum SynchronizationStatusValues {
39  
40      // TODO fix action icons
41  	DIRTY(SynchronizationStatus.DIRTY, n("dali.core.enums.synchronizationStatusValues.dirty"), "overlay-dirty"),
42      READY_TO_SYNCHRONIZE(SynchronizationStatus.READY_TO_SYNCHRONIZE, n("dali.core.enums.synchronizationStatusValues.dirty"), "overlay-dirty" /*mantis #26500*/),
43  	SYNCHRONIZED_FILE(SynchronizationStatus.SYNCHRONIZED_WITH_FILE, n("dali.core.enums.synchronizationStatusValues.synchroFile"), "overlay-synchro-file"),
44  	SYNCHRONIZED(SynchronizationStatus.SYNCHRONIZED ,n("dali.core.enums.synchronizationStatusValues.synchro"), "overlay-synchro"),
45  	DELETED(SynchronizationStatus.DELETED ,n("dali.core.enums.synchronizationStatusValues.deleted"), "overlay-deleted");
46  
47  	private final SynchronizationStatus synchronizationStatus;
48  	private final String keyLabel;
49  	private final String iconAction;
50  	
51  	SynchronizationStatusValues(SynchronizationStatus synchronizationStatus, String keyLabel, String iconAction) {
52          this.synchronizationStatus = synchronizationStatus;
53  		this.keyLabel = keyLabel;
54  		this.iconAction = iconAction;
55  	}
56  
57  	/**
58  	 * <p>Getter for the field <code>code</code>.</p>
59  	 *
60  	 * @return a {@link java.lang.String} object.
61  	 */
62  	public String getCode() {
63  		return this.synchronizationStatus.getValue();
64  	}
65  
66  	/**
67  	 * <p>getLabel.</p>
68  	 *
69  	 * @return a {@link java.lang.String} object.
70  	 */
71  	public String getLabel() {
72  		return t(this.keyLabel);
73  	}
74  	
75  	/**
76  	 * <p>Getter for the field <code>iconAction</code>.</p>
77  	 *
78  	 * @return a {@link java.lang.String} object.
79  	 */
80  	public String getIconAction() {
81  		return iconAction;
82  	}
83  
84      /**
85       * <p>toSynchronizationStatusDTO.</p>
86       *
87       * @return a {@link fr.ifremer.dali.dto.SynchronizationStatusDTO} object.
88       */
89      public SynchronizationStatusDTO toSynchronizationStatusDTO() {
90          SynchronizationStatusDTO dto = DaliBeanFactory.newSynchronizationStatusDTO();
91          dto.setId(ordinal());
92          dto.setCode(getCode());
93          dto.setName(getLabel());
94          dto.setIconName(getIconAction());
95          return dto;
96      }
97  
98      /**
99       * <p>equals.</p>
100      *
101      * @param synchronizationStatus a {@link fr.ifremer.dali.dto.SynchronizationStatusDTO} object.
102      * @return a boolean.
103      */
104     public boolean equals(SynchronizationStatusDTO synchronizationStatus) {
105         return synchronizationStatus != null && getCode().equals(synchronizationStatus.getCode());
106     }
107 
108     /**
109      * <p>getByCode.</p>
110      *
111      * @param code a {@link java.lang.String} object.
112      * @return a {@link fr.ifremer.dali.dto.enums.SynchronizationStatusValues} object.
113      */
114     public static SynchronizationStatusValues getByCode(String code) {
115         for (SynchronizationStatusValues enumValue: SynchronizationStatusValues.values()) {
116             if (StringUtils.isNotBlank(enumValue.getCode()) && enumValue.getCode().equals(code)) {
117                 return enumValue;
118             }
119         }
120         return null;
121     }
122 
123     /**
124      * <p>toSynchronizationStatusDTO.</p>
125      *
126      * @param code a {@link java.lang.String} object.
127      * @return a {@link fr.ifremer.dali.dto.SynchronizationStatusDTO} object.
128      */
129     public static SynchronizationStatusDTO toSynchronizationStatusDTO(String code) {
130         SynchronizationStatusValues enumValue = getByCode(code);
131         if (enumValue != null) {
132             return enumValue.toSynchronizationStatusDTO();
133         }
134         return null;
135     }
136 
137     /**
138      * <p>fromOrdinal.</p>
139      *
140      * @param ordinal a int.
141      * @return a {@link fr.ifremer.dali.dto.enums.SynchronizationStatusValues} object.
142      */
143     public static SynchronizationStatusValues fromOrdinal(int ordinal) {
144         if (ordinal > SynchronizationStatusValues.values().length -1) {
145             throw new IllegalArgumentException(String.format("Bad ordinal value [%s] for enumeration [%s]",
146                     ordinal,
147                     SynchronizationStatusValues.class.getSimpleName()));
148         }
149         return SynchronizationStatusValues.values()[ordinal];
150     }
151 }