-
Notifications
You must be signed in to change notification settings - Fork 1
/
DefaultProductPlugin.java
129 lines (111 loc) · 4.79 KB
/
DefaultProductPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright 2017-2022 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
*
* This file is part of REGARDS.
*
* REGARDS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* REGARDS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with REGARDS. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.cnes.regards.modules.acquisition.service.plugins;
import fr.cnes.regards.framework.module.rest.exception.ModuleException;
import fr.cnes.regards.framework.modules.plugins.annotations.Plugin;
import fr.cnes.regards.framework.modules.plugins.annotations.PluginParameter;
import fr.cnes.regards.modules.acquisition.plugins.IProductPlugin;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import java.nio.file.Path;
import java.util.List;
/**
* Compute the product name removing extension from filename
*
* @author Marc Sordi
*/
@Plugin(id = "DefaultProductPlugin",
version = "1.0.0-SNAPSHOT",
description = "Compute the product name from filename optionnaly removing extension or/and truncating product name",
author = "REGARDS Team",
contact = "regards@c-s.fr",
license = "GPLv3",
owner = "CSSI",
url = "https://github.com/RegardsOss")
public class DefaultProductPlugin implements IProductPlugin {
public static final String FIELD_REMOVE_EXT = "removeExtension";
public static final String FIELD_EXTS = "extensions";
public static final String FIELD_LENGTH = "maxLength";
public static final String FIELD_LENGTH_REQUIRED = "maxLengthRequired";
public static final String FIELD_PREFIX = "prefix";
@PluginParameter(name = FIELD_REMOVE_EXT,
label = "Enable extension removal",
description = "Remove file extension truncating from last dot index if no extension list specified",
optional = true)
private Boolean removeExtension;
@PluginParameter(name = FIELD_EXTS,
label = "List of extensions to remove",
description = "Full qualified extension strings",
optional = true)
private List<String> extensions;
@PluginParameter(name = FIELD_LENGTH,
label = "Max product name length",
description = "Product name is truncated to max length",
optional = true)
private Integer maxLength;
@PluginParameter(name = FIELD_LENGTH_REQUIRED,
label = "Throw error if max length not reached",
description = "Only available if max product name length is set",
defaultValue = "false",
optional = true)
private Boolean maxLengthRequired;
@PluginParameter(name = FIELD_PREFIX,
label = "Optional prefix",
description = "Prefix is concatenated with product name",
optional = true)
private String prefix;
@Override
public String getProductName(Path filePath) throws ModuleException {
String productName = filePath.getFileName().toString();
// Remove extension
if (BooleanUtils.isTrue(removeExtension)) {
int indexExtension = -1;
if (!CollectionUtils.isEmpty(extensions)) {
for (String extension : extensions) {
indexExtension = productName.lastIndexOf(extension);
if (indexExtension > 0) {
break;
}
}
} else {
indexExtension = productName.lastIndexOf('.');
}
if (indexExtension > 0) {
productName = productName.substring(0, indexExtension);
}
}
// Prefix
if (!StringUtils.isBlank(prefix)) {
productName = prefix + productName;
}
// Truncate
if (maxLength != null) {
if (productName.length() >= maxLength) {
productName = productName.substring(0, maxLength);
} else {
if (maxLengthRequired) {
throw new ModuleException(String.format("Product name \"%s\" is shorted than max length",
productName));
}
}
}
return productName;
}
}