package com.javaeye.jert.service.impl;
import java.util.List;
import com.javaeye.core.service.AbstractService;
import com.javaeye.core.service.GenericServiceException;
import com.javaeye.jert.domain.Database;
import com.javaeye.jert.domain.connection.ConnectionManager;
import com.javaeye.jert.service.DatabaseService;
/**
* @author Quake Wang
* @since 2004-12-19
* @version $Revision: 1.5 $
*/
public class DatabaseServiceDefaultImpl extends AbstractService implements DatabaseService {
private ConnectionManager connectionManager;
public Database getDatabase(Long id) {
Database db = (Database) loadById(Database.class, id);
//TODO: how to fix these code?
if(db == null) return null;
db.setConnectionManager(connectionManager);
return db;
}
public List getDatabases() {
return findAll(Database.class);
}
public void createDatabase(Database db) {
checkName(db);
create(db);
}
private void checkName(Database db) {
List sameNameDbs = findByNamedQuery("findSameNameDatabase", new Object[] { db.getName(), db.getId() });
if (sameNameDbs != null && sameNameDbs.size() > 0)
throw new GenericServiceException("error.same.name.db.exists");
}
public void updateDatabase(Database db) {
checkName(db);
update(db);
connectionManager.clearCache(db);
}
public void deleteDatabase(Database db) {
delete(db);
}
public void setConnectionManager(ConnectionManager connectionManager) {
this.connectionManager = connectionManager;
}
}