org.mustangproject.ZUGFeRD.UBLDAPullProvider Maven / Gradle / Ivy
Show all versions of library Show documentation
/**
* *********************************************************************
*
* Copyright 2018 Jochen Staerk
*
* Use is subject to license terms.
*
* 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.mustangproject.ZUGFeRD;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.mustangproject.EStandard;
import org.mustangproject.XMLTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UBLDAPullProvider implements IXMLProvider {
private static final Logger LOGGER = LoggerFactory.getLogger (UBLDAPullProvider.class);
protected IExportableTransaction trans;
protected TransactionCalculator calc;
byte[] ublData;
protected Profile profile = Profiles.getByName(EStandard.ubldespatchadvice, "basic", 1);
@Override
public void generateXML(IExportableTransaction trans) {
this.trans = trans;
this.calc = new TransactionCalculator(trans);
final SimpleDateFormat ublDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String xml = "\n" +
"\n" +
" 2.2 \n" +
" 1Lieferschein \n" +
" ubl-xml-only \n" +
" " + XMLTools.encodeXML(trans.getNumber()) + " \n" +
" " + ublDateFormat.format(trans.getIssueDate()) + " \n" +
" 900 \n";
if (trans.getReferenceNumber() != null) {
xml += " " + XMLTools.encodeXML(trans.getNumber()) + " \n";
}
xml += " " + getPartyXML(trans.getSender()) + " \n" +
" " + getPartyXML(trans.getRecipient()) + " \n";
if (trans.getDeliveryDate() != null) {
xml += " 1 " + ublDateFormat.format(trans.getDeliveryDate()) + " ";
}
int i = 1;
for (IZUGFeRDExportableItem item : trans.getZFItems()) {
xml +=
" \n" +
" " + XMLTools.encodeXML(Integer.toString(i++)) + " \n" +
" " + item.getQuantity() + " \n" +
" \n" +
" " + XMLTools.encodeXML(item.getBuyerOrderReferencedDocumentLineID()) + " \n" +
" \n" +
" \n" +
" " + XMLTools.encodeXML(item.getProduct().getName()) + " \n" +
" \n" +
" \n";
}
xml += " \n";
final byte[] ublRaw;
ublRaw = xml.getBytes(StandardCharsets.UTF_8);
ublData = XMLTools.removeBOM(ublRaw);
}
public String getPartyXML(IZUGFeRDExportableTradeParty tp) {
return "\n" +
" \n" +
" " + XMLTools.encodeXML(tp.getLocation()) + " \n" +
" \n" +
" " + XMLTools.encodeXML(tp.getName()) + " \n" +
" \n" +
" \n" +
" " + XMLTools.encodeXML(tp.getStreet()) + " \n" +
" \n" +
" \n" +
" " + XMLTools.encodeXML(tp.getCountry()) + " \n" +
" \n" +
" \n" +
" ";
}
@Override
public byte[] getXML() {
byte[] res = ublData;
final StringWriter sw = new StringWriter();
Document document = null;
try {
document = DocumentHelper.parseText(new String(ublData));
} catch (final DocumentException e1) {
LOGGER.error ("Failed to parse UBL", e1);
}
try {
final OutputFormat format = OutputFormat.createPrettyPrint();
format.setTrimText(false);
final XMLWriter writer = new XMLWriter(sw, format);
writer.write(document);
res = sw.toString().getBytes(StandardCharsets.UTF_8);
} catch (final IOException e) {
LOGGER.error ("Failed to write XML", e);
}
return res;
}
@Override
public void setTest() {
}
@Override
public void setProfile(Profile p) {
profile = p;
}
@Override
public Profile getProfile() {
return profile;
}
}