| import java.io.IOException; |
| import java.nio.charset.Charset; |
| |
| import org.apache.commons.httpclient.HttpClient; |
| import org.apache.commons.httpclient.HttpException; |
| import org.apache.commons.httpclient.methods.GetMethod; |
| import org.apache.commons.httpclient.methods.PostMethod; |
| import org.apache.commons.httpclient.methods.PutMethod; |
| import java.io.ByteArrayInputStream; |
| |
| public class BestellAkte { |
| |
| private final String narsUrl; |
| |
| public BestellAkte(String narsUrl) { |
| this.narsUrl = narsUrl; |
| } |
| |
| public void syncAgorumWithNotes(EinkaufsTicket ekTicket) throws Exception { |
| ekTicket.createLock(); |
| if (!isCommunicationWithAgorumNecessary(ekTicket.getBestellStatus())) { |
| ekTicket.setAgorumStatus("Keine Bestellakte vorhanden."); |
| } else { |
| try { |
| if (!exists(ekTicket.getBestellNummer())) { |
| createAgorumBestellAkte(ekTicket); |
| } else { |
| updateAgorumBestellAkte(ekTicket); |
| } |
| ekTicket.setAgorumUrl(getAgorumUrl(ekTicket.getBestellNummer())); |
| ekTicket.setAgorumStatus("OK"); |
| } catch (IOException e) { |
| ekTicket.setAgorumStatus("Fehler bei der Kommunikation mit NARS! "+e.getMessage()); |
| } |
| } |
| ekTicket.saveAndReleaseLock(); |
| if (ekTicket.errorsOnSave()) { |
| throw new Exception("Errors while saving document in notes! " + ekTicket.getBestellNummer()); |
| } |
| } |
| |
| private ByteArrayInputStream generateRequestBody(EinkaufsTicket ekTicket) { |
| return new ByteArrayInputStream(("{\"bestellNummer\": \"" + ekTicket.getBestellNummer() + "\", " |
| + "\"bestellStatus\": \"" + ekTicket.getBestellStatus() + "\", " |
| + "\"bestellGenehmiger\": \"" + ekTicket.getBestellGenehmiger() + "\", " |
| + "\"bestellSumme\": \"" + ekTicket.getBestellSumme() + "\", " |
| + "\"bestellGenehmigungsDatum\": \"" + ekTicket.getBestellGenehmigungsDatum() + "\", " |
| + "\"bestellDatum\": \"" + ekTicket.getBestellDatum() + "\", " |
| + "\"projektnummer\": \"" + ekTicket.getProjektnummer() + "\", " |
| + "\"projektnummerJahr\": \"" + ekTicket.getProjektnummerJahr()+ "\", " |
| + "\"kostenstelle\": \"" + ekTicket.getKostenstelle() + "\", " |
| + "\"kostenstelleJahr\": \"" + ekTicket.getKostenstelleJahr()+ "\", " |
| + "\"notesLink\": \"" + ekTicket.getNotesUrl() + "\" " |
| + "}").getBytes(Charset.forName("UTF-8"))); |
| } |
| |
| private String createAgorumBestellAkte(EinkaufsTicket ekTicket) throws HttpException, IOException { |
| PostMethod method = new PostMethod(narsUrl + "bestellakte/"); |
| HttpClient client = new HttpClient(); |
| method.setRequestBody(generateRequestBody(ekTicket)); |
| method.addRequestHeader("Accept", "text/plain;charset=UTF-8"); |
| method.addRequestHeader("Content-Type", "application/json"); |
| int statusCode = client.executeMethod(method); |
| if (statusCode != 201) { |
| throw new HttpException("Fehler beim Anlegen der Bestellakte: createAgorumBestellAkte gibt StatusCode ["+statusCode+"]"); |
| } |
| byte[] responseBody = method.getResponseBody(); |
| return new String(responseBody); |
| } |
| |
| private void updateAgorumBestellAkte(EinkaufsTicket ekTicket) throws HttpException, IOException { |
| PutMethod method = new PutMethod(narsUrl + "bestellakte/"+ekTicket.getBestellNummer()); |
| HttpClient client = new HttpClient(); |
| method.setRequestBody(generateRequestBody(ekTicket)); |
| method.addRequestHeader("Accept", "text/plain;charset=UTF-8"); |
| method.addRequestHeader("Content-Type", "application/json"); |
| int statusCode = client.executeMethod(method); |
| if (statusCode != 204) { |
| throw new HttpException("Fehler beim Aktualisieren der Bestellakte: updateAgorumBestellAkte gibt StatusCode ["+statusCode+"]"); |
| } |
| } |
| |
| private boolean exists(String bestellNummer) throws HttpException, IOException { |
| GetMethod method = new GetMethod(narsUrl + "bestellakte/" + bestellNummer); |
| HttpClient client = new HttpClient(); |
| int statusCode = client.executeMethod(method); |
| return statusCode == 200; |
| } |
| |
| private String getAgorumUrl(String bestellNummer) throws HttpException, IOException { |
| GetMethod method = new GetMethod(narsUrl + "bestellakte/" + bestellNummer + "/link"); |
| HttpClient client = new HttpClient(); |
| int statusCode = client.executeMethod(method); |
| if (statusCode != 200) { |
| throw new HttpException("Fehler bei der Kommunikation mit NARS! getAgorumUrl gibt StatusCode ["+statusCode+"]"); |
| } |
| byte[] responseBody = method.getResponseBody(); |
| return new String(responseBody); |
| } |
| |
| private boolean isCommunicationWithAgorumNecessary(String bestellStatus) { |
| return bestellStatus.equals("Bestellung") |
| || bestellStatus.equals("Bestätigung") || bestellStatus.equals("Teillieferung") |
| || bestellStatus.equals("Lieferung") || bestellStatus.equals("Rechnung") || bestellStatus.equals("obsolet"); |
| } |
| |
| } |