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.Filter;
24  import org.waarp.openr66.dao.LimitDAO;
25  import org.waarp.openr66.dao.exception.DAOConnectionException;
26  import org.waarp.openr66.dao.exception.DAONoDataException;
27  import org.waarp.openr66.pojo.Limit;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.concurrent.ConcurrentHashMap;
32  
33  public class XMLLimitDAO implements LimitDAO {
34  
35    /**
36     * HashTable in case of lack of database
37     */
38    private static final ConcurrentHashMap<String, Limit>
39        dbR66ConfigurationHashMap = new ConcurrentHashMap<String, Limit>();
40  
41    public XMLLimitDAO() {
42      // Empty
43    }
44  
45    @Override
46    public void close() {
47      // ignore
48    }
49  
50    @Override
51    public void delete(final Limit limit) {
52      dbR66ConfigurationHashMap.remove(limit.getHostid());
53    }
54  
55    @Override
56    public void deleteAll() {
57      dbR66ConfigurationHashMap.clear();
58    }
59  
60    @Override
61    public List<Limit> getAll() throws DAOConnectionException {
62      return new ArrayList<Limit>(dbR66ConfigurationHashMap.values());
63    }
64  
65    @Override
66    public boolean exist(final String hostid) throws DAOConnectionException {
67      return dbR66ConfigurationHashMap.containsKey(hostid);
68    }
69  
70    @Override
71    public List<Limit> 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<Limit> 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<Limit> 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<Limit> 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<Limit> find(final List<Filter> filters, final String field,
97                            final boolean asc, final int limit, final int offset)
98        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 dbR66ConfigurationHashMap.size();
117     }
118     throw new DAOConnectionException("Operation not supported on XML DAO");
119   }
120 
121   @Override
122   public void insert(final Limit limit) {
123     dbR66ConfigurationHashMap.put(limit.getHostid(), limit);
124   }
125 
126   @Override
127   public Limit select(final String hostid)
128       throws DAOConnectionException, DAONoDataException {
129     final Limit limit = dbR66ConfigurationHashMap.get(hostid);
130     if (limit != null) {
131       return limit;
132     }
133     throw new DAONoDataException("Limit not found");
134   }
135 
136   @Override
137   public void update(final Limit limit) {
138     dbR66ConfigurationHashMap.put(limit.getHostid(), limit);
139   }
140 }