View Javadoc

1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH Munich, Germany (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo.proxy;
24  
25  import java.util.Calendar;
26  import java.util.Iterator;
27  
28  import lotus.domino.Agent;
29  import lotus.domino.AgentContext;
30  import lotus.domino.Database;
31  import lotus.domino.DateTime;
32  import lotus.domino.Document;
33  import lotus.domino.DocumentCollection;
34  import lotus.domino.NotesException;
35  import de.bea.domingo.DAgent;
36  import de.bea.domingo.DAgentContext;
37  import de.bea.domingo.DBase;
38  import de.bea.domingo.DDatabase;
39  import de.bea.domingo.DDocument;
40  import de.bea.domingo.DNotesMonitor;
41  import de.bea.domingo.DSession;
42  
43  /***
44   * Represents the agent environment of the current program, if an agent is running it.
45   *
46   * @author <a href="mailto:kriede@users.sourceforge.net">Kurt Riede</a>
47   */
48  public final class AgentContextProxy extends BaseProxy implements DAgentContext {
49  
50      /*** serial version ID for serialization. */
51      private static final long serialVersionUID = 3257288032582580016L;
52  
53      /***
54       * Creates a new instance of this class.
55       *
56       * @param theFactory the controlling factory
57       * @param theParent the parent object
58       * @param object the notes object
59       * @param monitor the monitor
60       */
61      private AgentContextProxy(final NotesProxyFactory theFactory, final DBase theParent, final AgentContext object,
62              final DNotesMonitor monitor) {
63          super(theFactory, theParent, object, monitor);
64      }
65  
66      /***
67       * Factory method for instances of this class.
68       *
69       * @param theFactory the controlling factory
70       * @param session the session that produced the database
71       * @param theAgentContext Notes agent context object
72       * @param monitor the monitor that handles logging
73       * @return Returns instance of DAgentContext
74       */
75      public static DAgentContext getInstance(final NotesProxyFactory theFactory, final SessionProxy session,
76              final AgentContext theAgentContext, final DNotesMonitor monitor) {
77          if (theAgentContext == null) {
78              return null;
79          }
80          final AgentContextProxy agentContextProxy = new AgentContextProxy(theFactory, session, theAgentContext, monitor);
81          return agentContextProxy;
82      }
83  
84      /***
85       * {@inheritDoc}
86       * @see de.bea.domingo.DAgentContext#updateProcessedDoc(de.bea.domingo.DDocument)
87       */
88      public void updateProcessedDoc(final DDocument document) {
89          Document notesDocument = (Document) ((DocumentProxy) document).getNotesObject();
90          try {
91              getAgentContext().updateProcessedDoc(notesDocument);
92          } catch (NotesException e) {
93              throw newRuntimeException(RESOURCES.getString("agent.context.cannot.mark.processed"), e);
94          }
95      }
96  
97      /***
98       * {@inheritDoc}
99       * @see de.bea.domingo.DAgentContext#unprocessedFTSearch(java.lang.String, int)
100      */
101     public Iterator unprocessedFTSearch(final String query, final int maxDocs) {
102         try {
103             final DocumentCollection collection = getAgentContext().unprocessedFTSearch(query, maxDocs);
104             return new DocumentCollectionIterator(getFactory(), this, collection, getMonitor());
105         } catch (NotesException e) {
106             throw newRuntimeException(RESOURCES.getString("agent.context.cannot.ftsearchunprocessed"), e);
107         }
108     }
109 
110     /***
111      * {@inheritDoc}
112      * @see de.bea.domingo.DAgentContext#unprocessedFTSearch(java.lang.String, int, int, int)
113      */
114     public Iterator unprocessedFTSearch(final String query, final int maxDocs, final int sortOpt, final int otherOpt) {
115         try {
116             final DocumentCollection collection = getAgentContext().unprocessedFTSearch(query, maxDocs, sortOpt, otherOpt);
117             return new DocumentCollectionIterator(getFactory(), this, collection, getMonitor());
118         } catch (NotesException e) {
119             throw newRuntimeException(RESOURCES.getString("agent.context.cannot.ftsearchunprocessed"), e);
120         }
121     }
122 
123     /***
124      * {@inheritDoc}
125      * @see de.bea.domingo.DAgentContext#unprocessedSearch(java.lang.String, java.util.Calendar, int)
126      */
127     public Iterator unprocessedSearch(final String query, final Calendar dateTime, final int maxDocs) {
128         try {
129             final DateTime notesDateTime = createDateTime(dateTime);
130             final DocumentCollection collection = getAgentContext().unprocessedSearch(query, notesDateTime, maxDocs);
131             getFactory().recycle(dateTime);
132             return new DocumentCollectionIterator(getFactory(), this, collection, getMonitor());
133         } catch (NotesException e) {
134             throw newRuntimeException(RESOURCES.getString("agent.context.cannot.searchunprocessed"), e);
135         }
136     }
137 
138     /***
139      * {@inheritDoc}
140      * @see de.bea.domingo.DAgentContext#getEffectiveUserName()
141      */
142     public String getEffectiveUserName() {
143         try {
144             return getAgentContext().getEffectiveUserName();
145         } catch (NotesException e) {
146             throw newRuntimeException(RESOURCES.getString("agent.context.cannot.get.effectiveusername"), e);
147         }
148     }
149 
150     /***
151      * {@inheritDoc}
152      * @see de.bea.domingo.DAgentContext#getCurrentAgent()
153      */
154     public DAgent getCurrentAgent() {
155         try {
156             final DatabaseProxy database = (DatabaseProxy) getCurrentDatabase();
157             final Agent currentAgent = getAgentContext().getCurrentAgent();
158             return AgentProxy.getInstance(getFactory(), database, currentAgent, getMonitor());
159         } catch (NotesException e) {
160             throw newRuntimeException(RESOURCES.getString("agent.context.cannot.get.currentagent"), e);
161         }
162     }
163 
164     /***
165      * {@inheritDoc}
166      * @see de.bea.domingo.DAgentContext#getCurrentDatabase()
167      */
168     public DDatabase getCurrentDatabase() {
169         try {
170             final DSession session = (DSession) getParent();
171             final Database currentDatabase = getAgentContext().getCurrentDatabase();
172             return DatabaseProxy.getInstance(getFactory(), session, currentDatabase, getMonitor(), true);
173         } catch (NotesException e) {
174             throw newRuntimeException(RESOURCES.getString("agent.context.cannot.get.currentdatabase"), e);
175         }
176     }
177 
178     /***
179      * {@inheritDoc}
180      * @see de.bea.domingo.DAgentContext#getDocumentContext()
181      */
182     public DDocument getDocumentContext() {
183         try {
184             final DatabaseProxy database = (DatabaseProxy) getCurrentDatabase();
185             final Document documentContext = getAgentContext().getDocumentContext();
186             return (DDocument) BaseDocumentProxy.getInstance(getFactory(), database, documentContext, getMonitor());
187         } catch (NotesException e) {
188             throw newRuntimeException(RESOURCES.getString("agent.context.cannot.get.documentcontext"), e);
189         }
190     }
191 
192     /***
193      * {@inheritDoc}
194      * @see de.bea.domingo.DAgentContext#getLastExitStatus()
195      */
196     public int getLastExitStatus() {
197         try {
198             return getAgentContext().getLastExitStatus();
199         } catch (NotesException e) {
200             throw newRuntimeException(RESOURCES.getString("agent.context.cannot.get.last.exitstatus"), e);
201         }
202     }
203 
204     /***
205      * {@inheritDoc}
206      * @see de.bea.domingo.DAgentContext#getLastRun()
207      */
208     public Calendar getLastRun() {
209         try {
210             final DateTime dateTime = getAgentContext().getLastRun();
211             if (dateTime != null) {
212                 final Calendar calendar = createCalendar(dateTime);
213                 getFactory().recycle(dateTime);
214                 dateTime.recycle();
215                 return calendar;
216             }
217             return null;
218         } catch (NotesException e) {
219             throw newRuntimeException(RESOURCES.getString("agent.context.cannot.get.lastrun"), e);
220         }
221     }
222 
223     /***
224      * {@inheritDoc}
225      * @see de.bea.domingo.DAgentContext#getSavedData()
226      */
227     public DDocument getSavedData() {
228         try {
229             final Document savedData = getAgentContext().getSavedData();
230             return (DDocument) BaseDocumentProxy.getInstance(getFactory(), getCurrentDatabase(), savedData, getMonitor());
231         } catch (NotesException e) {
232             throw newRuntimeException(RESOURCES.getString("agent.context.cannot.get.saveddata"), e);
233         }
234     }
235 
236     /***
237      * {@inheritDoc}
238      * @see de.bea.domingo.DAgentContext#getUnprocessedDocuments()
239      */
240     public Iterator getUnprocessedDocuments() {
241         try {
242             final DocumentCollection collection = getAgentContext().getUnprocessedDocuments();
243             return new DocumentCollectionIterator(getFactory(), this, collection, getMonitor());
244         } catch (NotesException e) {
245             throw newRuntimeException(RESOURCES.getString("agent.context.cannot.searchunprocessed"), e);
246         }
247     }
248 
249     /***
250      * @see java.lang.Object#toString()
251      * @return "[DAgentContext]"
252      */
253     public String toString() {
254         return "[DAgentContext]";
255     }
256 
257     /***
258      * Returns the associated Notes agent context.
259      *
260      * @return associated Notes agent context
261      */
262     private AgentContext getAgentContext() {
263         getFactory().preprocessMethod();
264         return (AgentContext) this.getNotesObject();
265     }
266 }