View Javadoc

1   /* ***************************************************************************
2    * This library is part of INFONOVA OpenCms Modules.
3    * Common Modules for the Open Source Content Management System: OpenCms.
4    * 
5    * Copyright (c) 2010 INFONOVA GmbH, Austria.
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   * 
17   * For further information about INFONOVA GmbH, Austria, please
18   * see the company website: http://www.infonova.at/
19   *
20   * For further information about INFONOVA OpenCms Modules, please see the
21   * project website: http://sourceforge.net/projects/bp-cms-commons/
22   * 
23   * You should have received a copy of the GNU Lesser General Public
24   * License along with this library; if not, write to the Free Software
25   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26   *****************************************************************************/
27  package at.infonova.opencms.modules.commons.dao;
28  
29  /**
30   * Exception that is thrown if more than the expected number of rows was
31   * returned by a query.
32   *
33   * @author Christian Graf
34   * @author Andreas Gabriel
35   */
36  public class TooManyRowsFoundException extends RuntimeException {
37  
38      private static final long serialVersionUID = -4813523462856418753L;
39  
40      /**
41       * constructor for the exception that is created if too many rows where found for
42       * the specified entity with the specified ID
43       *
44       * @param entityName the name of the entity (e.g. User)
45       * @param entityId the ID of the entity that was used for loading it
46       */
47      public TooManyRowsFoundException(String entityName, long entityId) {
48          this(entityName, "id", entityId);
49      }
50  
51      /**
52       * constructor for the exception that is created if too many rows where found for
53       * the specified entity with the specified key and value
54       *
55       * @param entityName the name of the entity that was loaded (e.g. CustomerService)
56       * @param keyName the name of the key column that was used for limiting the results (e.g. externalKey)
57       * @param keyValue the value of the key column that was used for limiting the results (e.g. 12345)
58       */
59      public TooManyRowsFoundException(String entityName, String keyName, Object keyValue) {
60          this(entityName, new String[]{keyName}, new Object[]{keyValue});
61      }
62  
63      /**
64       * constructor for the exception that is created if too many rows where found for
65       * the specified entity with the specified keys and values
66       *
67       * @param entityName the name of the entity that was loaded (e.g. CustomerService)
68       * @param keyNames the names of the key colums that were used for limiting the results (e.g. externalKey)
69       * @param keyValues the value of the key columns that were used for limiting the results (e.g. 12345)
70       */
71      public TooManyRowsFoundException(String entityName, String[] keyNames, Object[] keyValues) {
72          super(createErrorMessage(entityName, keyNames, keyValues));
73      }
74  
75      private final static String createErrorMessage(String entityName, String[] keyNames, Object[] keyValues) {
76          StringBuilder sb = new StringBuilder();
77          sb.append("Too many rows found for ");
78          for (int i = 0; i < keyNames.length; i++) {
79              if (i > 0) {
80                  sb.append(" and ");
81              }
82              sb.append(entityName);
83              sb.append('.');
84              sb.append(keyNames[i]);
85              sb.append('=');
86              sb.append(keyValues[i]);
87          }
88  
89          return sb.toString();
90      }
91  }