Domino 9 und frühere Versionen > ND9: Entwicklung

Frage zu Java Agenten

(1/5) > >>

Bruce Willis:
Hallo,

ich hätte eine Frage zu Java Spezialisten...

Nachdem ein Kollege den u.g. Java Agenten in einer Datenbank programmiert hat, entstehen dort fast täglich Replizier- und Speicherkonflikte... :(
Kommt evtl. was verdächtig vor?

Vielen Dank im Voraus.

Gruß
Leo


--- Code: ---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");
    }
    
}

--- Ende Code ---


--- Code: ---public class NotesDateConverter {

    public static String convertNotesDateToJavaFormat(final String eingabeValue) {
        if (eingabeValue.length() == 0) {
            return eingabeValue;
        }
        try {
            String rueckgabeValue = eingabeValue;
            int pos = eingabeValue.indexOf(" ");
            if (pos > 0) {
                rueckgabeValue = eingabeValue.substring(0, pos);
            }
            pos = rueckgabeValue.indexOf("/");
            if (pos > 0) {
                rueckgabeValue = rueckgabeValue.replace("/", ".");
            }
            pos = rueckgabeValue.indexOf(".");
            if (pos == 1) {
                rueckgabeValue = "0"+rueckgabeValue;
            }
            pos = rueckgabeValue.indexOf(".", 3);
            if (pos == 4) {
                rueckgabeValue = rueckgabeValue.substring(0, 3) + "0" + rueckgabeValue.substring(3);
            }
            if (rueckgabeValue.substring(6).length() == 2) {
                rueckgabeValue = rueckgabeValue.substring(0, 6) + "20" + rueckgabeValue.substring(6);
            }
            return rueckgabeValue;
        } catch (StringIndexOutOfBoundsException e) {
            return "Error: "+e.toString();
        }
    }

    public static String entfernePunkt(String eingabeValue) {
            String rueckgabeValue = eingabeValue;
            int pos = eingabeValue.indexOf(".");
            if (pos > 0) {
                    rueckgabeValue = eingabeValue.substring(0, pos);
            }
            return rueckgabeValue;
    }
}

--- Ende Code ---


--- Code: ---import lotus.domino.*;

public class BestellAkteInAgorumPflegen extends AgentBase {

    public void NotesMain() {
      try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
          EinkaufsTicketNotesImpl ekTicket = new EinkaufsTicketNotesImpl(agentContext.getDocumentContext());
          new BestellAkte("http://nars-prod-1.osl.MyDomain.de:4380/").syncAgorumWithNotes(ekTicket);
      } catch(Exception e) {
          e.printStackTrace();
      }
   }
}


--- Ende Code ---



--- Code: ---public interface EinkaufsTicket {

String getBestellNummer();
String getBestellStatus();
        String getBestellGenehmiger();
        String getBestellSumme();
        String getBestellGenehmigungsDatum();
        String getNotesUrl();
        String getBestellDatum();
        String getKostenstelle();
        String getKostenstelleJahr();
        String getProjektnummer();
        String getProjektnummerJahr();
void createLock();
void saveAndReleaseLock();
void setAgorumUrl(String agorumUrl);
void setAgorumStatus(String agorumStatus);
boolean errorsOnSave();

}


--- Ende Code ---


--- Code: ---import lotus.domino.*;

public class EinkaufsTicketNotesImpl implements EinkaufsTicket {

    private final Document notesDocument;
    private final String bestellNummer;
    private final String bestellStatus;
    private final String bestellGenehmiger;
    private final String bestellSumme;
    private final String bestellGenehmigungsDatum;
    private final String notesUrl;
    private final String bestellDatum;
    private boolean exceptionOnSave = false;
    private final String kostenstelle;
    private final String kostenstelleJahr;
    private final String projektnummer;
    private final String projektnummerJahr;

    public EinkaufsTicketNotesImpl(Document notesDocument) throws NotesException {        
            this.notesDocument = notesDocument;        
            this.bestellNummer = (String) notesDocument.getItemValue("RequestNumber").elementAt(0);        
            this.bestellStatus = (String) notesDocument.getItemValue("bStatus").elementAt(0);        
            this.bestellGenehmiger = (String) notesDocument.getItemValue("approval_control_authorized_name").elementAt(0);        
            this.bestellSumme = notesDocument.getItemValue("authorized_betrag").elementAt(0).toString();        
            this.bestellGenehmigungsDatum = NotesDateConverter.convertNotesDateToJavaFormat((String) notesDocument.getItemValue("approval_control_authorized_date").elementAt(0));        
            this.bestellDatum = NotesDateConverter.convertNotesDateToJavaFormat(notesDocument.getItemValueDateTimeArray("vDat_3").elementAt(0).toString());        
            this.kostenstelle = NotesDateConverter.entfernePunkt(notesDocument.getItemValue("numK").elementAt(0).toString());        
            this.kostenstelleJahr = NotesDateConverter.entfernePunkt(notesDocument.getItemValue("jahrK").elementAt(0).toString());        
            this.projektnummer = NotesDateConverter.entfernePunkt(notesDocument.getItemValue("numP").elementAt(0).toString());        
            this.projektnummerJahr = NotesDateConverter.entfernePunkt(notesDocument.getItemValue("jahrP").elementAt(0).toString());        
            this.notesUrl = notesDocument.getNotesURL();    
    }    

    public String getBestellNummer() {
        return bestellNummer;
    }

    public String getBestellStatus() {
        return bestellStatus;
    }

    public String getBestellGenehmiger() {
        return this.bestellGenehmiger;
    }

    public String getBestellSumme() {
        return this.bestellSumme.trim();
    }

    public String getBestellGenehmigungsDatum() {
        return this.bestellGenehmigungsDatum;
    }

    public String getNotesUrl() {
        return this.notesUrl;
    }
    
    public String getBestellDatum() {
        return this.bestellDatum;
    }

    public void createLock() {
        try {
            notesDocument.lock();
        } catch (NotesException e) {
            exceptionOnSave = true;
        }
    }

    public void saveAndReleaseLock() {
        try {
            notesDocument.computeWithForm(false, false);
            notesDocument.save();
            notesDocument.unlock();
        } catch (NotesException e) {
            exceptionOnSave = true;
        }
    }

    public void setAgorumUrl(String agorumUrl) {
        try {
            notesDocument.replaceItemValue("agorumBestellakteUrl", agorumUrl);
        } catch (Exception e) {
            exceptionOnSave = true;
        }
    }

    public void setAgorumStatus(String agorumStatus) {
        try {
            notesDocument.replaceItemValue("agorumStatus", agorumStatus);
        } catch (NotesException e) {
            exceptionOnSave = true;
        }
    }

    public boolean errorsOnSave() {
        return exceptionOnSave;
    }

    public String getKostenstelle() {
        return this.kostenstelle;
    }

    public String getKostenstelleJahr() {
        return this.kostenstelleJahr;
    }

    public String getProjektnummer() {
        return this.projektnummer;
    }

    public String getProjektnummerJahr() {
        return this.projektnummerJahr;
    }
}

--- Ende Code ---

jBubbleBoy:

--- Zitat von: Bruce Willis am 01.03.18 - 10:48:17 ---public void createLock() {
        try {
            notesDocument.lock();
        } catch (NotesException e) {
            exceptionOnSave = true;
        }
    }
/quote]

"lock" gibt true oder false zurück, je nachdem ob das Sperren erfolgreich war oder nicht,
und das wird bei euch ignoriert - böse ;)
 

--- Ende Zitat ---

Bruce Willis:

--- Zitat von: jBubbleBoy am 01.03.18 - 12:52:20 ---public void createLock() {
        try {
            notesDocument.lock();
        } catch (NotesException e) {
            exceptionOnSave = true;
        }
    }

--- Ende Zitat ---

Hallo Erik,
Danke!
Muss man dies an einer bestimmten Stelle oder egal wo eintragen?
 

Bruce Willis:
Noch eine Frage hinterher...

Der Kollege hatte den Agenten-Aufruf in PostSave eingetragen:


--- Code: ---@Command([ToolsRunMacro];"bestellAkteInAgorumPflegen");

--- Ende Code ---

Ich würde es gerne in QuerySave z.B. so verschieben


--- Code: --- '01.03.2018 Agorum Agent wird hier statt POSTSAVE Event augerufen!!!
Dim s As New NotesSession
Dim db As NotesDatabase
Dim agent2 As NotesAgent
Set db = s.CurrentDatabase
Set agent2 = db.GetAgent("bestellAkteInAgorumPflegen")
If agent2.Run = 0 Then
Print "Agorum-Agent erfolgreich gelaufen"
Else
MessageBox "Agorum-Agent konnte nicht gestartet werden",64, "Bitte Ihrem Notes Admin melden"
End If

--- Ende Code ---

Aber bekomme die Fehlermeldung "Could not execute Macro".
Auch dann, wenn ich den Agenten per Button mit LS aufrufe.

Außerdem, wenn ich den Agenten per Button mit FS aufrufe, bekomme ich die Fehlermeldung "Dokument noch nicht gespeichert".

Was ist da noch faul?

jBubbleBoy:
eins nach dem anderen

zur 1. Frage, das ist ein Stück Code aus deinem 1. Beitrag.
Frage: Was soll denn passieren wenn ein Dokument gesperrt ist?

Navigation

[0] Themen-Index

[#] Nächste Seite

Zur normalen Ansicht wechseln