001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.dataAccess;
019
020import io.stallion.dataAccess.filtering.FilterChain;
021import io.stallion.dataAccess.filtering.FilterOperator;
022import io.stallion.exceptions.UsageException;
023
024import java.util.List;
025import java.util.Set;
026
027/**
028 * A wrapper around a ModelController that only gives access to read methods.
029 * Used for sandboxed contexts where the plugin or template might only have
030 * read access, not write access.
031 *
032 * @param <T>
033 */
034public class ReadOnlyWrapper<T extends Model> implements ModelController<T> {
035
036    private ModelController<T> original;
037
038    public ReadOnlyWrapper(ModelController<T> original) {
039        this.original = original;
040    }
041
042
043    @Override
044    public void init(DataAccessRegistration registration, Persister<T> persister, Stash<T> stash) {
045
046    }
047
048    @Override
049    public String getBucket() {
050        return original.getBucket();
051    }
052
053
054    @Override
055    public T detach(T obj) {
056        throw new UsageException("This controller is wrapped to be read-only.");
057
058    }
059
060
061
062    @Override
063    public void save(T obj) {
064        throw new UsageException("This controller is wrapped to be read-only.");
065    }
066
067
068    @Override
069    public void softDelete(T obj) {
070        throw new UsageException("This controller is wrapped to be read-only.");
071    }
072
073    @Override
074    public void hardDelete(T obj) {
075        throw new UsageException("This controller is wrapped to be read-only.");
076    }
077
078
079
080    @Override
081    public void onPreRead() {
082        throw new UsageException("This controller is wrapped to be read-only.");
083    }
084
085
086    @Override
087    public void onPreSavePrepare(T obj) {
088
089    }
090
091    @Override
092    public void onPreSaveValidate(T obj) {
093
094    }
095
096    @Override
097    public void onPreCreatePrepare(T obj) {
098
099    }
100
101    @Override
102    public void onPreCreateValidate(T obj) {
103
104    }
105
106    @Override
107    public void onPostSave(T obj) {
108
109    }
110
111    @Override
112    public void onPostCreate(T obj) {
113
114    }
115
116    @Override
117    public void onPostLoadItem(T obj) {
118
119    }
120
121    @Override
122    public FilterChain<T> filterChain() {
123        return original.filterChain();
124    }
125
126    @Override
127    public FilterChain<T> filter(String name, Object value) {
128        return original.filter(name, value);
129    }
130
131    @Override
132    public FilterChain<T> filter(String name, Object value, String op) {
133        return original.filter(name, value, op);
134    }
135
136    @Override
137    public FilterChain<T> filterBy(String name, Object value, FilterOperator op) {
138        return original.filterBy(name, value, op);
139    }
140
141    @Override
142    public FilterChain<T> filterByKey(String keyName, Object value) {
143        return original.filterByKey(keyName, value);
144    }
145
146
147    @Override
148    public T forIdWithDeleted(Long id) {
149        return original.forIdWithDeleted(id);
150    }
151
152    @Override
153    public T originalForId(T id) {
154        return null;
155    }
156
157    @Override
158    public T forId(Long id) {
159        return original.forId(id);
160    }
161
162    @Override
163    public T originalForId(Long id) {
164        return original.originalForId(id);
165    }
166
167    @Override
168    public T forUniqueKey(String keyName, Object value) {
169        return original.forUniqueKey(keyName, value);
170    }
171
172
173    @Override
174    public List<T> listForKey(String keyName, Object value) {
175        return original.listForKey(keyName, value);
176    }
177
178    @Override
179    public int countForKey(String keyName, Object value) {
180        return original.countForKey(keyName, value);
181    }
182
183    @Override
184    public Set<String> getKeyFields() {
185        return original.getKeyFields();
186    }
187
188
189    @Override
190    public Set<String> getUniqueFields() {
191        return original.getUniqueFields();
192    }
193
194
195    @Override
196    public List<T> all() {
197        return original.all();
198    }
199
200    @Override
201    public Persister<T> getPersister() {
202        throw new UsageException("This controller is wrapped to be read-only.");
203    }
204
205    @Override
206    public Stash<T> getStash() {
207        throw new UsageException("This controller is wrapped to be read-only.");
208    }
209
210    @Override
211    public Class<T> getModelClass() {
212        return original.getModelClass();
213    }
214
215    @Override
216    public Boolean isWritable() {
217        return false;
218    }
219
220    @Override
221    public void reset() {
222        throw new UsageException("This controller is wrapped to be read-only.");
223    }
224
225    @Override
226    public ReadOnlyWrapper<T> getReadonlyWrapper() {
227        return this;
228    }
229}