| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.config; |
|---|
| 34 | |
|---|
| 35 | import java.util.Collection; |
|---|
| 36 | |
|---|
| 37 | import javax.ejb.ObjectNotFoundException; |
|---|
| 38 | |
|---|
| 39 | import org.jcoderz.commons.InternalErrorException; |
|---|
| 40 | import org.jcoderz.commons.EntityNotFoundException; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Static access methods for the |
|---|
| 45 | * {@link org.jcoderz.commons.config.ConfigEntity} Entity Bean. |
|---|
| 46 | * |
|---|
| 47 | * With this helper Configuration Entries (ConfigEntity) can be found by |
|---|
| 48 | * there primary key, or all entries will be selected. |
|---|
| 49 | * |
|---|
| 50 | * For performance issues the ConfigEntities are available as common |
|---|
| 51 | * Entity Beans that are readable and writable, and as ReadOnly EntityBeans |
|---|
| 52 | * (ConfigReaderEntity) which are only readable and will be automatically |
|---|
| 53 | * by the application server. |
|---|
| 54 | * Therefore the different finders are provided for both types. |
|---|
| 55 | * |
|---|
| 56 | * The ReadOnly Pattern is generated by ant target 'add-readonly-entities' |
|---|
| 57 | * and its generator task 'make-readonly-beans'. |
|---|
| 58 | * |
|---|
| 59 | */ |
|---|
| 60 | public final class ConfigEntityHelper |
|---|
| 61 | { |
|---|
| 62 | private ConfigEntityHelper () |
|---|
| 63 | { |
|---|
| 64 | // no instances allowed - only static helper methods |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * Finds a ConfigEntity by |
|---|
| 69 | * {@link java.lang.String} (primary key). |
|---|
| 70 | * @param id the configuration key id. |
|---|
| 71 | * @return the ConfigEntity denoted by the given PK. |
|---|
| 72 | * @throws EntityNotFoundException if no entity can be found by the |
|---|
| 73 | * given primary key |
|---|
| 74 | * @throws InternalErrorException if a Remote, Finder or Naming |
|---|
| 75 | * exception occurs |
|---|
| 76 | */ |
|---|
| 77 | public static ConfigEntity findByPrimaryKey (String id) |
|---|
| 78 | throws EntityNotFoundException, InternalErrorException |
|---|
| 79 | { |
|---|
| 80 | return findByPrimaryKey(id, false); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Finds a ConfigEntity as ReadOnly by |
|---|
| 85 | * {@link java.lang.String} (primary key). |
|---|
| 86 | * Remark that this instance is only a ReadOnly entity bean! |
|---|
| 87 | * @param id the configuration key id. |
|---|
| 88 | * @return the ConfigEntity denoted by the given PK. |
|---|
| 89 | * @throws EntityNotFoundException if no entity can be found by the |
|---|
| 90 | * given primary key |
|---|
| 91 | * @throws InternalErrorException if a Remote, Finder or Naming |
|---|
| 92 | * exception occurs |
|---|
| 93 | */ |
|---|
| 94 | public static ConfigEntity findReadOnlyByPrimaryKey (String id) |
|---|
| 95 | throws EntityNotFoundException, InternalErrorException |
|---|
| 96 | { |
|---|
| 97 | return findByPrimaryKey(id, true); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Returns all participants data. |
|---|
| 102 | * @return a collection with all all participants data. |
|---|
| 103 | * @throws InternalErrorException if a Remote, Finder or Naming |
|---|
| 104 | * exception occurs |
|---|
| 105 | */ |
|---|
| 106 | public static Collection findAll () |
|---|
| 107 | throws InternalErrorException |
|---|
| 108 | { |
|---|
| 109 | return findAll(false); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | * Returns all participants data as Read Only instances. |
|---|
| 114 | * Remark that retrieved instances are ReadOnly! |
|---|
| 115 | * @return a collection with all all participants data. |
|---|
| 116 | * @throws InternalErrorException if a Remote, Finder or Naming |
|---|
| 117 | * exception occurs |
|---|
| 118 | */ |
|---|
| 119 | public static Collection findAllReadOnly () |
|---|
| 120 | throws InternalErrorException |
|---|
| 121 | { |
|---|
| 122 | return findAll(true); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * Finds a ConfigEntity by |
|---|
| 127 | * {@link java.lang.String} (primary key). |
|---|
| 128 | * @param id the configuration key id. |
|---|
| 129 | * @param readOnly true, if a read only instance should be retrieved; false |
|---|
| 130 | * for a read-write instance. |
|---|
| 131 | * @return the ConfigEntity denoted by the given PK. |
|---|
| 132 | * @throws EntityNotFoundException if no entity can be found by the |
|---|
| 133 | * given primary key |
|---|
| 134 | * @throws InternalErrorException if a Remote, Finder or Naming |
|---|
| 135 | * exception occurs |
|---|
| 136 | */ |
|---|
| 137 | private static ConfigEntity findByPrimaryKey (String id, boolean readOnly) |
|---|
| 138 | throws EntityNotFoundException, InternalErrorException |
|---|
| 139 | { |
|---|
| 140 | final ConfigEntityPK pk = new ConfigEntityPK(id.toString()); |
|---|
| 141 | ConfigEntity result; |
|---|
| 142 | try |
|---|
| 143 | { |
|---|
| 144 | if (!readOnly) |
|---|
| 145 | { |
|---|
| 146 | result = ConfigEntityUtil.getHome().findByPrimaryKey(pk); |
|---|
| 147 | } |
|---|
| 148 | else |
|---|
| 149 | { |
|---|
| 150 | result = ConfigReaderEntityUtil.getHome().findByPrimaryKey(pk); |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | catch (ObjectNotFoundException e) |
|---|
| 154 | { |
|---|
| 155 | throw new EntityNotFoundException(ConfigEntityBean.TABLE_NAME, |
|---|
| 156 | "findByPrimaryKey", id, ConfigEntityBean.class.getName(), |
|---|
| 157 | e); |
|---|
| 158 | } |
|---|
| 159 | // remaining exceptions: Remote, Finder, Naming |
|---|
| 160 | catch (Exception e) |
|---|
| 161 | { |
|---|
| 162 | throw new InternalErrorException("DB access failed", e); |
|---|
| 163 | } |
|---|
| 164 | return result; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | /** |
|---|
| 168 | * Returns all participants data. |
|---|
| 169 | * @param readOnly true, if Entity Beans should be ReadOnly. False for |
|---|
| 170 | * read-write instances. |
|---|
| 171 | * @return a collection with all all participants data. |
|---|
| 172 | * @throws InternalErrorException if a Remote, Finder or Naming |
|---|
| 173 | * exception occurs |
|---|
| 174 | */ |
|---|
| 175 | public static Collection findAll (boolean readOnly) |
|---|
| 176 | throws InternalErrorException |
|---|
| 177 | { |
|---|
| 178 | Collection result; |
|---|
| 179 | try |
|---|
| 180 | { |
|---|
| 181 | if (!readOnly) |
|---|
| 182 | { |
|---|
| 183 | result = ConfigEntityUtil.getHome().findAll(); |
|---|
| 184 | } |
|---|
| 185 | else |
|---|
| 186 | { |
|---|
| 187 | result = ConfigReaderEntityUtil.getHome().findAll(); |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | // remaining exceptions: Remote, Finder, Naming |
|---|
| 191 | catch (Exception e) |
|---|
| 192 | { |
|---|
| 193 | throw new InternalErrorException("DB access failed", e); |
|---|
| 194 | } |
|---|
| 195 | return result; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|