View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.event.xml.query;
19  
20  import org.w3c.dom.Element;
21  import at.ac.tuwien.infosys.sm4all.copal.api.ContextQuery;
22  import at.ac.tuwien.infosys.sm4all.copal.api.ProcessedEventQuery;
23  import at.ac.tuwien.infosys.sm4all.copal.api.RedefinitionOfQueryException;
24  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.ParsingException;
25  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Unmarshaller;
26  import at.ac.tuwien.infosys.sm4all.copal.service.copal.ContextQueryFactory;
27  
28  /**
29   * Command that unmarshalls a {@link ProcessedEventQuery} from an
30   * {@link Element}.
31   * 
32   * @author sanjin
33   */
34  public class XMLProcessedEventQuery extends XMLContextQuery implements
35          Unmarshaller<ProcessedEventQuery> {
36  
37      private final ContextQueryFactory factory;
38      private final Element element;
39  
40      /**
41       * Creates instance of {@link ProcessedEventQuery} unmarshaller which uses
42       * specified {@link Element} to unmarshal a {@link ProcessedEventQuery} and
43       * specified {@link ContextQueryFactory} to construct the
44       * {@link ProcessedEventQuery}.
45       * 
46       * @param factory the {@link ContextQueryFactory} used for constructing the
47       *        unmarshalled {@link ProcessedEventQuery}.
48       * @param element the {@link Element} used for unmarshalling.
49       * @throws NullPointerException if specified {@link Element} is
50       *         <code>null</code>.
51       */
52      public XMLProcessedEventQuery(final ContextQueryFactory factory,
53              final Element element) {
54          super(element);
55  
56          if (factory == null)
57              throw new NullPointerException("Query factory cannot be null.");
58  
59          this.factory = factory;
60          this.element = element;
61      }
62  
63      /**
64       * Unmarshalls a {@link ProcessedEventQuery} from specified {@link Element}.
65       * 
66       * @return the unmarshalled {@link ProcessedEventQuery}.
67       * @throws ParsingException if unmarshalling was unsuccessful.
68       * @throws RedefinitionOfQueryException if a {@link ContextQuery} with same
69       *         name and different event type or criteria was already created
70       *         with specified {@link ContextQueryFactory}.
71       */
72      @Override
73      public ProcessedEventQuery unmarshal() throws ParsingException,
74              RedefinitionOfQueryException {
75          final String name = new Name(this.element).unmarshal();
76          final String event = new Event(this.element).unmarshal();
77          final String criteria = new Criteria(this.element).unmarshal();
78  
79          final ProcessedEventQuery result;
80  
81          if (criteria == null)
82              result = this.factory.create(name, event);
83          else
84              result = this.factory.create(name, event, criteria);
85  
86          return result;
87      }
88  }