Skip to content

Commit d56877c

Browse files
Expose UBL utils (#17)
* expose ubl utils to static methods * change release.sh
1 parent 033eccb commit d56877c

File tree

4 files changed

+156
-65
lines changed

4 files changed

+156
-65
lines changed

release.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,22 @@
2020
echo "===================================="
2121
read -p "Enter Release: " releaseVersion
2222
read -p "Enter Next Release (SNAPSHOT): " nextVersion
23-
read -p "Enter Github Username: " username
24-
read -s -p "Enter Github Password: " password
23+
read -s -p "Enter Github Token: " token
2524

2625
# Update to release
2726
mvn versions:set -DnewVersion="$releaseVersion"
2827

2928
git add .
3029
git commit -m "Release $releaseVersion"
31-
git push "https://$username:$password@github.com/project-openubl/xsender.git"
30+
git push "https://$token@github.com/project-openubl/xsender.git"
3231

3332
# Create tag and push
3433
git tag "$releaseVersion"
35-
git push "https://$username:$password@github.com/project-openubl/xsender.git" --tags
34+
git push "https://$token@github.com/project-openubl/xsender.git" --tags
3635

3736
# Create next snapshot
3837
mvn versions:set -DnewVersion="$nextVersion-SNAPSHOT"
3938

4039
git add .
4140
git commit -m "Prepare next release $nextVersion-SNAPSHOT"
42-
git push "https://$username:$password@github.com/project-openubl/xsender.git"
41+
git push "https://$token@github.com/project-openubl/xsender.git"

src/main/java/io/github/project/openubl/xmlsenderws/webservices/managers/smart/SmartBillServiceManager.java

Lines changed: 15 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import io.github.project.openubl.xmlsenderws.webservices.exceptions.InvalidXMLFileException;
2121
import io.github.project.openubl.xmlsenderws.webservices.exceptions.UnsupportedDocumentTypeException;
2222
import io.github.project.openubl.xmlsenderws.webservices.managers.BillServiceManager;
23+
import io.github.project.openubl.xmlsenderws.webservices.models.DeliveryURLType;
2324
import io.github.project.openubl.xmlsenderws.webservices.providers.BillServiceModel;
25+
import io.github.project.openubl.xmlsenderws.webservices.utils.UBLUtils;
2426
import io.github.project.openubl.xmlsenderws.webservices.wrappers.ServiceConfig;
2527
import io.github.project.openubl.xmlsenderws.webservices.xml.DocumentType;
2628
import io.github.project.openubl.xmlsenderws.webservices.xml.XmlContentModel;
@@ -33,15 +35,10 @@
3335
import java.io.IOException;
3436
import java.nio.file.Files;
3537
import java.nio.file.Path;
36-
import java.text.MessageFormat;
3738
import java.util.Optional;
38-
import java.util.regex.Pattern;
3939

4040
public class SmartBillServiceManager {
4141

42-
private final static String FILENAME_FORMAT1 = "{0}-{1}-{2}";
43-
private final static String FILENAME_FORMAT2 = "{0}-{1}";
44-
4542
private SmartBillServiceManager() {
4643
// Just static methods
4744
}
@@ -70,7 +67,8 @@ public static SmartBillServiceModel send(byte[] file, String username, String pa
7067
DocumentType documentType = documentTypeOptional.get();
7168

7269
String deliveryURL = getDeliveryURL(documentType, xmlContentModel);
73-
String fileNameWithoutExtension = getFileNameWithoutExtension(documentType, xmlContentModel.getRuc(), xmlContentModel.getDocumentID());
70+
String fileNameWithoutExtension = UBLUtils.getFileNameWithoutExtension(documentType, xmlContentModel.getRuc(), xmlContentModel.getDocumentID())
71+
.orElseThrow(() -> new IllegalStateException("Invalid type of UBL Document, can not extract Serie-Numero to create fileName"));
7472

7573
ServiceConfig config = new ServiceConfig.Builder()
7674
.url(deliveryURL)
@@ -137,64 +135,21 @@ public static BillServiceModel getStatus(String ticket, XmlContentModel xmlConte
137135

138136
private static String getDeliveryURL(DocumentType documentType, XmlContentModel xmlContentModel) {
139137
SmartBillServiceConfig config = SmartBillServiceConfig.getInstance();
140-
switch (documentType) {
141-
case INVOICE:
142-
case CREDIT_NOTE:
143-
case DEBIT_NOTE:
144-
case SUMMARY_DOCUMENT:
145-
return config.getInvoiceAndNoteDeliveryURL();
146-
case VOIDED_DOCUMENT:
147-
String tipoDocumentoAfectado = xmlContentModel.getVoidedLineDocumentTypeCode();
148-
Catalogo1 catalog1 = Catalogo1.valueOfCode(tipoDocumentoAfectado).orElseThrow(() -> new IllegalStateException("No se pudo convertir el valor del catálogo"));
149-
if (catalog1.equals(Catalogo1.PERCEPCION) || catalog1.equals(Catalogo1.RETENCION)) {
150-
return config.getPerceptionAndRetentionDeliveryURL();
151-
} else if (catalog1.equals(Catalogo1.GUIA_REMISION_REMITENTE)) {
152-
return config.getDespatchAdviceDeliveryURL();
153-
}
138+
139+
DeliveryURLType deliveryURLType = UBLUtils.getDeliveryURLType(documentType, xmlContentModel)
140+
.orElseThrow(() -> new IllegalStateException("Invalid type of UBL Document, can not create Sunat Server URL"));
141+
142+
switch (deliveryURLType) {
143+
case BASIC_DOCUMENTS_URL:
154144
return config.getInvoiceAndNoteDeliveryURL();
155-
case PERCEPTION:
156-
case RETENTION:
157-
return config.getPerceptionAndRetentionDeliveryURL();
158-
case DESPATCH_ADVICE:
145+
case DESPATCH_DOCUMENT_URL:
159146
return config.getDespatchAdviceDeliveryURL();
147+
case PERCEPTION_RETENTION_URL:
148+
return config.getPerceptionAndRetentionDeliveryURL();
160149
default:
161-
throw new IllegalStateException("Invalid type of UBL Document, can not create Sunat Server URL");
150+
throw new IllegalStateException("DeliveryURLType not configured to return a value");
162151
}
163152
}
164153

165-
private static String getFileNameWithoutExtension(DocumentType type, String ruc, String documentID) {
166-
String codigoDocumento;
167-
switch (type) {
168-
case INVOICE:
169-
if (Pattern.compile("^[F|f].*$").matcher(documentID).find()) {
170-
codigoDocumento = Catalogo1.FACTURA.getCode();
171-
} else if (Pattern.compile("^[B|b].*$").matcher(documentID).find()) {
172-
codigoDocumento = Catalogo1.BOLETA.getCode();
173-
} else {
174-
throw new IllegalStateException("Invalid Serie, can not detect code");
175-
}
176-
177-
return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
178-
case CREDIT_NOTE:
179-
codigoDocumento = Catalogo1.NOTA_CREDITO.getCode();
180-
return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
181-
case DEBIT_NOTE:
182-
codigoDocumento = Catalogo1.NOTA_DEBITO.getCode();
183-
return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
184-
case VOIDED_DOCUMENT:
185-
case SUMMARY_DOCUMENT:
186-
return MessageFormat.format(FILENAME_FORMAT2, ruc, documentID);
187-
case PERCEPTION:
188-
codigoDocumento = Catalogo1.PERCEPCION.getCode();
189-
return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
190-
case RETENTION:
191-
codigoDocumento = Catalogo1.RETENCION.getCode();
192-
return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
193-
case DESPATCH_ADVICE:
194-
codigoDocumento = Catalogo1.GUIA_REMISION_REMITENTE.getCode();
195-
return MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
196-
default:
197-
throw new IllegalStateException("Invalid type of UBL Document, can not extract Serie-Numero to create fileName");
198-
}
199-
}
154+
200155
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Eclipse Public License - v 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.eclipse.org/legal/epl-2.0/
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.xmlsenderws.webservices.models;
18+
19+
public enum DeliveryURLType {
20+
BASIC_DOCUMENTS_URL,
21+
DESPATCH_DOCUMENT_URL,
22+
PERCEPTION_RETENTION_URL
23+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Eclipse Public License - v 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.eclipse.org/legal/epl-2.0/
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.github.project.openubl.xmlsenderws.webservices.utils;
18+
19+
import io.github.project.openubl.xmlsenderws.webservices.catalogs.Catalogo1;
20+
import io.github.project.openubl.xmlsenderws.webservices.models.DeliveryURLType;
21+
import io.github.project.openubl.xmlsenderws.webservices.xml.DocumentType;
22+
import io.github.project.openubl.xmlsenderws.webservices.xml.XmlContentModel;
23+
24+
import java.text.MessageFormat;
25+
import java.util.Optional;
26+
import java.util.regex.Pattern;
27+
28+
public class UBLUtils {
29+
30+
private final static String FILENAME_FORMAT1 = "{0}-{1}-{2}";
31+
private final static String FILENAME_FORMAT2 = "{0}-{1}";
32+
33+
public UBLUtils() {
34+
// Only static methods
35+
}
36+
37+
public static Optional<DeliveryURLType> getDeliveryURLType(DocumentType documentType, XmlContentModel xmlContentModel) {
38+
DeliveryURLType result = null;
39+
switch (documentType) {
40+
case INVOICE:
41+
case CREDIT_NOTE:
42+
case DEBIT_NOTE:
43+
case SUMMARY_DOCUMENT:
44+
result = DeliveryURLType.BASIC_DOCUMENTS_URL;
45+
break;
46+
case VOIDED_DOCUMENT:
47+
String tipoDocumentoAfectado = xmlContentModel.getVoidedLineDocumentTypeCode();
48+
Catalogo1 catalog1 = Catalogo1.valueOfCode(tipoDocumentoAfectado).orElseThrow(() -> new IllegalStateException("No se pudo convertir el valor del catálogo"));
49+
if (catalog1.equals(Catalogo1.PERCEPCION) || catalog1.equals(Catalogo1.RETENCION)) {
50+
result = DeliveryURLType.PERCEPTION_RETENTION_URL;
51+
} else if (catalog1.equals(Catalogo1.GUIA_REMISION_REMITENTE)) {
52+
result = DeliveryURLType.DESPATCH_DOCUMENT_URL;
53+
} else {
54+
result = DeliveryURLType.BASIC_DOCUMENTS_URL;
55+
}
56+
break;
57+
case PERCEPTION:
58+
case RETENTION:
59+
result = DeliveryURLType.PERCEPTION_RETENTION_URL;
60+
break;
61+
case DESPATCH_ADVICE:
62+
result = DeliveryURLType.DESPATCH_DOCUMENT_URL;
63+
break;
64+
}
65+
66+
return Optional.ofNullable(result);
67+
}
68+
69+
public static Optional<String> getFileNameWithoutExtension(DocumentType type, String ruc, String documentID) {
70+
String result = null;
71+
72+
String codigoDocumento;
73+
switch (type) {
74+
case INVOICE:
75+
if (Pattern.compile("^[F|f].*$").matcher(documentID).find()) {
76+
codigoDocumento = Catalogo1.FACTURA.getCode();
77+
} else if (Pattern.compile("^[B|b].*$").matcher(documentID).find()) {
78+
codigoDocumento = Catalogo1.BOLETA.getCode();
79+
} else {
80+
throw new IllegalStateException("Invalid Serie, can not detect code");
81+
}
82+
83+
result = MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
84+
break;
85+
case CREDIT_NOTE:
86+
codigoDocumento = Catalogo1.NOTA_CREDITO.getCode();
87+
result = MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
88+
break;
89+
case DEBIT_NOTE:
90+
codigoDocumento = Catalogo1.NOTA_DEBITO.getCode();
91+
result = MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
92+
break;
93+
case VOIDED_DOCUMENT:
94+
case SUMMARY_DOCUMENT:
95+
result = MessageFormat.format(FILENAME_FORMAT2, ruc, documentID);
96+
break;
97+
case PERCEPTION:
98+
codigoDocumento = Catalogo1.PERCEPCION.getCode();
99+
result = MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
100+
break;
101+
case RETENTION:
102+
codigoDocumento = Catalogo1.RETENCION.getCode();
103+
result = MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
104+
break;
105+
case DESPATCH_ADVICE:
106+
codigoDocumento = Catalogo1.GUIA_REMISION_REMITENTE.getCode();
107+
result = MessageFormat.format(FILENAME_FORMAT1, ruc, codigoDocumento, documentID);
108+
break;
109+
}
110+
111+
return Optional.ofNullable(result);
112+
}
113+
114+
}

0 commit comments

Comments
 (0)