Commit 3ec5cb8d authored by Maxym Borodenko's avatar Maxym Borodenko

Merge branch '96-flexible-accession-and-inventorynumber' into 'master'

Resolve "Flexible accession and inventoryNumber"

Closes #96

See merge request !104
parents bb7310fb 0ed5d21b
......@@ -21,6 +21,7 @@ import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.gringlobal.component.GGCE;
import org.gringlobal.spring.GMTStringToDateConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -54,6 +55,7 @@ public class ApplicationConfig {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
pspc.setIgnoreResourceNotFound(true);
pspc.setFileEncoding("UTF-8");
pspc.setTrimValues(false); // this is also the default
final List<Resource> locations = new ArrayList<>();
locations.add(new ClassPathResource("application.properties"));
locations.add(new ClassPathResource("gringlobal.properties"));
......@@ -104,4 +106,9 @@ public class ApplicationConfig {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
return factory.getValidator();
}
@Bean
public GGCE ggceHelper() {
return new GGCE();
}
}
/*
* Copyright 2020 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gringlobal.component;
import java.text.MessageFormat;
import org.apache.commons.lang3.StringUtils;
import org.gringlobal.model.Accession;
import org.gringlobal.model.Inventory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
/**
* The Class GGCE.
*/
public class GGCE implements InitializingBean {
private static GGCE _instance;
@Value("${accession.format1}")
private String accessionPart1Format;
@Value("${accession.format2}")
private String accessionPart2Format;
@Value("${accession.format3}")
private String accessionPart3Format;
@Value("${inventory.format1}")
private String inventoryPart1Format;
@Value("${inventory.format2}")
private String inventoryPart2Format;
@Value("${inventory.format3}")
private String inventoryPart3Format;
@Value("${inventory.format4}")
private String inventoryPart4Format;
@Override
public void afterPropertiesSet() throws Exception {
System.err.println("Accesion number format: " + this.accessionNumber("ACC", 99999l, "A"));
System.err.println("Inventory number format: " + this.inventoryNumber("INV", 313313l, "B", "SD"));
GGCE._instance = this;
}
public static String accessionNumber(Accession accession) {
if (accession == null) {
return null;
}
if (_instance != null) {
return _instance.accessionNumber(accession.getAccessionNumberPart1(), accession.getAccessionNumberPart2(), accession.getAccessionNumberPart3());
}
StringBuilder sb = new StringBuilder(accession.getAccessionNumberPart1());
if (accession.getAccessionNumberPart2() != null) {
sb.append(" ").append(accession.getAccessionNumberPart2());
}
if (StringUtils.isNotBlank(accession.getAccessionNumberPart3())) {
sb.append(" ").append(accession.getAccessionNumberPart3());
}
return sb.toString();
}
private String accessionNumber(String accessionNumberPart1, Long accessionNumberPart2, String accessionNumberPart3) {
StringBuilder sb = new StringBuilder(MessageFormat.format(accessionPart1Format, accessionNumberPart1));
if (accessionNumberPart2 != null) {
sb.append(MessageFormat.format(accessionPart2Format, accessionNumberPart2));
}
if (StringUtils.isNotBlank(accessionNumberPart3)) {
sb.append(MessageFormat.format(accessionPart3Format, accessionNumberPart3));
}
return sb.toString().replaceAll("\\s+", " ").trim();
}
public static String inventoryNumber(Inventory inventory) {
if (inventory == null) {
return null;
}
if (_instance != null) {
return _instance.inventoryNumber(inventory.getInventoryNumberPart1(), inventory.getInventoryNumberPart2(), inventory.getInventoryNumberPart3(), inventory
.getFormTypeCode());
}
StringBuilder sb = new StringBuilder(inventory.getInventoryNumberPart1());
if (inventory.getInventoryNumberPart2() != null) {
sb.append(" ").append(inventory.getInventoryNumberPart2());
}
if (StringUtils.isNotBlank(inventory.getInventoryNumberPart3())) {
sb.append(" ").append(inventory.getInventoryNumberPart3());
}
sb.append(" ").append(inventory.getFormTypeCode());
return sb.toString();
}
private String inventoryNumber(String inventoryNumberPart1, Long inventoryNumberPart2, String inventoryNumberPart3, String formTypeCode) {
StringBuilder sb = new StringBuilder(MessageFormat.format(inventoryPart1Format, inventoryNumberPart1));
if (inventoryNumberPart2 != null) {
sb.append(MessageFormat.format(inventoryPart2Format, inventoryNumberPart2));
}
if (StringUtils.isNotBlank(inventoryNumberPart3)) {
sb.append(MessageFormat.format(inventoryPart3Format, inventoryNumberPart3));
}
sb.append(MessageFormat.format(inventoryPart4Format, formTypeCode));
return sb.toString().replaceAll("\\s+", " ").trim();
}
}
......@@ -21,10 +21,10 @@ import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.JsonViews;
import org.gringlobal.component.elastic.ElasticTrigger;
import org.gringlobal.component.GGCE;
import org.gringlobal.component.elastic.AppContextHelper;
import org.gringlobal.component.elastic.ElasticTrigger;
import org.gringlobal.custom.elasticsearch.ElasticLoader;
import org.gringlobal.custom.elasticsearch.IgnoreField;
import org.gringlobal.custom.validation.javax.CodeValueField;
......@@ -461,14 +461,7 @@ public class Accession extends CooperatorOwnedModel implements ElasticLoader, El
}
public String getAccessionNumber() {
StringBuilder sb = new StringBuilder(this.accessionNumberPart1);
if (this.accessionNumberPart2 != null) {
sb.append(" ").append(this.accessionNumberPart2);
}
if (StringUtils.isNotBlank(this.accessionNumberPart3)) {
sb.append(" ").append(this.accessionNumberPart3);
}
return sb.toString();
return GGCE.accessionNumber(this);
}
@Transient
......
......@@ -23,9 +23,9 @@ import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.JsonViews;
import org.gringlobal.component.GGCE;
import org.gringlobal.component.elastic.ElasticTrigger;
import org.gringlobal.custom.elasticsearch.ElasticLoader;
import org.gringlobal.custom.elasticsearch.IgnoreField;
......@@ -697,18 +697,7 @@ public class Inventory extends CooperatorOwnedModel implements Copyable<Inventor
}
public String getInventoryNumber() {
StringBuilder sb = new StringBuilder(this.inventoryNumberPart1);
if (this.inventoryNumberPart2 != null) {
sb.append(" ").append(this.inventoryNumberPart2);
}
if (StringUtils.isNotBlank(this.inventoryNumberPart3)) {
sb.append(" ").append(this.inventoryNumberPart3);
}
sb.append(" ").append(this.formTypeCode);
return sb.toString();
return GGCE.inventoryNumber(this);
}
@Override
......
......@@ -31,6 +31,14 @@ base.cookie-http-only=true
# UI URL
frontend.url=http://${host.name}:3000
# Important!
accession.format1={0}
accession.format2=\ {0,number,####}
accession.format3=\ {0}
inventory.format1={0}
inventory.format2=\ {0,number,####}
inventory.format3=\ {0}
inventory.format4=\ {0}
#db.url=jdbc:mysql://localhost/gringlobal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
#db.driverClassName=com.mysql.jdbc.Driver
......
......@@ -25,6 +25,7 @@ import org.gringlobal.application.config.DatabaseConfig;
import org.gringlobal.application.config.SchedulerConfig;
import org.gringlobal.application.config.SecurityConfig;
import org.gringlobal.application.config.SpringMvcConfig;
import org.gringlobal.component.GGCE;
import org.gringlobal.spring.ApplicationStartup;
import org.gringlobal.spring.GMTStringToDateConverter;
import org.springframework.context.annotation.Bean;
......@@ -66,6 +67,7 @@ public final class ApplicationConfig {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
pspc.setIgnoreResourceNotFound(true);
pspc.setFileEncoding("UTF-8");
pspc.setTrimValues(false); // this is also the default
final List<Resource> locations = new ArrayList<>();
locations.add(new ClassPathResource("application.properties"));
locations.add(new ClassPathResource("junit.properties"));
......@@ -102,6 +104,10 @@ public final class ApplicationConfig {
return factory.getValidator();
}
@Bean
public GGCE ggceHelper() {
return new GGCE();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment