View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.dao.xml;
22  
23  import org.waarp.openr66.dao.BusinessDAO;
24  import org.waarp.openr66.dao.Filter;
25  import org.waarp.openr66.dao.exception.DAOConnectionException;
26  import org.waarp.openr66.dao.exception.DAONoDataException;
27  import org.waarp.openr66.pojo.Business;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.concurrent.ConcurrentHashMap;
32  
33  public class XMLBusinessDAO implements BusinessDAO {
34  
35    /**
36     * HashTable in case of lack of database
37     */
38    private static final ConcurrentHashMap<String, Business>
39        dbR66BusinessHashMap = new ConcurrentHashMap<String, Business>();
40  
41    public XMLBusinessDAO() {
42      // Empty
43    }
44  
45    @Override
46    public void close() {
47      // ignore
48    }
49  
50    @Override
51    public void delete(final Business business) {
52      dbR66BusinessHashMap.remove(business.getHostid());
53    }
54  
55    @Override
56    public void deleteAll() {
57      dbR66BusinessHashMap.clear();
58    }
59  
60    @Override
61    public List<Business> getAll() throws DAOConnectionException {
62      return new ArrayList<Business>(dbR66BusinessHashMap.values());
63    }
64  
65    @Override
66    public boolean exist(final String hostid) throws DAOConnectionException {
67      return dbR66BusinessHashMap.containsKey(hostid);
68    }
69  
70    @Override
71    public List<Business> find(final List<Filter> fitlers)
72        throws DAOConnectionException {
73      throw new DAOConnectionException("Operation not supported on XML DAO");
74    }
75  
76    @Override
77    public List<Business> find(final List<Filter> filters, final int limit)
78        throws DAOConnectionException {
79      throw new DAOConnectionException("Operation not supported on XML DAO");
80    }
81  
82    @Override
83    public List<Business> find(final List<Filter> filters, final String field,
84                               final boolean asc) throws DAOConnectionException {
85      throw new DAOConnectionException("Operation not supported on XML DAO");
86    }
87  
88    @Override
89    public List<Business> find(final List<Filter> filters, final String field,
90                               final boolean asc, final int limit)
91        throws DAOConnectionException {
92      throw new DAOConnectionException("Operation not supported on XML DAO");
93    }
94  
95    @Override
96    public List<Business> find(final List<Filter> filters, final String field,
97                               final boolean asc, final int limit,
98                               final int offset) throws DAOConnectionException {
99      throw new DAOConnectionException("Operation not supported on XML DAO");
100   }
101 
102   @Override
103   public void update(final List<Filter> filters, final String toSet)
104       throws DAOConnectionException {
105     throw new DAOConnectionException("Operation not supported on XML DAO");
106   }
107 
108   /**
109    * {@link DAOConnectionException}
110    *
111    * @return count only if filters is empty or null
112    */
113   @Override
114   public long count(final List<Filter> fitlers) throws DAOConnectionException {
115     if (fitlers == null || fitlers.isEmpty()) {
116       return dbR66BusinessHashMap.size();
117     }
118     throw new DAOConnectionException("Operation not supported on XML DAO");
119   }
120 
121   @Override
122   public void insert(final Business business) {
123     dbR66BusinessHashMap.put(business.getHostid(), business);
124   }
125 
126   @Override
127   public Business select(final String hostid)
128       throws DAOConnectionException, DAONoDataException {
129     final Business business = dbR66BusinessHashMap.get(hostid);
130     if (business != null) {
131       return business;
132     }
133     throw new DAONoDataException("Business not found");
134   }
135 
136   @Override
137   public void update(final Business business) {
138     dbR66BusinessHashMap.put(business.getHostid(), business);
139   }
140 }