1 package fr.ifremer.tutti.persistence.entities.referential;
2
3 /*
4 * #%L
5 * Tutti :: Persistence
6 * $Id:$
7 * $HeadURL:$
8 * %%
9 * Copyright (C) 2012 - 2014 Ifremer
10 * %%
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation, either version 3 of the
14 * License, or (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 General Public
22 * License along with this program. If not, see
23 * <http://www.gnu.org/licenses/gpl-3.0.html>.
24 * #L%
25 */
26
27 import com.google.common.base.Function;
28 import com.google.common.base.Preconditions;
29 import com.google.common.base.Predicate;
30 import fr.ifremer.adagio.core.dao.technical.hibernate.TemporaryDataHelper;
31
32 public class Vessels extends AbstractVessels {
33
34 // public static Map<String, Vessel> splitByRegistrationCode(List<Vessel> programs) {
35 // return Maps.uniqueIndex(programs, GET_REGISTRATION_CODE);
36 // }
37 //
38 // public static Map<String, Vessel> splitByInternationalRegistrationCode(List<Vessel> programs) {
39 // return Maps.uniqueIndex(programs, GET_INTERNAL_REGISTRATION_CODE);
40 // }
41
42 public static final Predicate<Vessel> IS_TEMPORARY = Vessels::isTemporary;
43
44 public static Function<Vessel, String> GET_REGISTRATION_CODE_OR_INTERNATIONAL_REGISTRATION_CODE = input -> {
45
46 String result = input.getRegistrationCode();
47 if (result == null) {
48 result = input.getInternationalRegistrationCode();
49 }
50 return result;
51 };
52
53 // public static final Function<Vessel, String> GET_REGISTRATION_CODE = Vessel::getRegistrationCode;
54
55 public static final Function<Vessel, String> GET_INTERNAL_REGISTRATION_CODE = Vessel::getInternationalRegistrationCode;
56
57 /**
58 * Is the given {@code vessel} a temporary data ?
59 *
60 * @param vessel vessel to test
61 * @return {@code true} if the given {@code vessel} is temporary
62 * @since 3.8
63 */
64 public static boolean isTemporary(Vessel vessel) {
65
66 Preconditions.checkNotNull(vessel);
67 Preconditions.checkNotNull(vessel.getId());
68
69 return TuttiReferentialEntities.isStatusTemporary(vessel) && isTemporaryId(vessel.getId());
70
71 }
72
73 /**
74 * Is the given {@code id} is a vessel temporary id ?
75 *
76 * @param id id to test
77 * @return {@code true} if the id is a temporary vessel id
78 * @since 3.14
79 */
80 public static boolean isTemporaryId(String id) {
81
82 Preconditions.checkNotNull(id);
83 return id.startsWith(TemporaryDataHelper.TEMPORARY_NAME_PREFIX);
84
85 }
86 }