
org.droitateddb.ConnectedEntityService Maven / Gradle / Ivy
/*
* Copyright (C) 2014 The droitated DB Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.droitateddb;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import org.droitateddb.entity.Entity;
import java.io.Closeable;
/**
* Provides a version of the {@link EntityService} with an open connection to the {@link SQLiteDatabase}. Make sure to
* close the connection with {@link ConnectedEntityService#close()} when you finished using the service.
*
* @param Entity, for which this service will be used
* @author Falk Appel
* @author Alexander Frank
*/
public class ConnectedEntityService extends EntityService implements Closeable {
private final SQLiteDatabase database;
/**
* Creates a {@link ConnectedEntityService} for the given {@link Entity}
*
* @param context Android context
* @param entityClass Class of the {@link Entity}, the service should be used for
* @throws IllegalArgumentException When the given {@link #entityClass} is no {@link Entity}
*/
public ConnectedEntityService(final Context context, final Class entityClass) {
super(context, entityClass);
this.database = dbCreator.getDatabaseConnection();
}
/*
* Provides test access to constructor
*/
ConnectedEntityService(final Context context, final Class entityClass, SQLiteDatabase database) {
super(context, entityClass);
this.database = database;
}
@Override
protected void closeDB(SQLiteDatabase database) {
// nothing todo
}
@Override
protected SQLiteDatabase openDB() {
return database;
}
@Override
public void close() {
dbCreator.reduceDatabaseConnection();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy