2011-03-22 23:02:06 +01:00
|
|
|
package de.hsadmin.mods.db;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
2011-05-20 16:57:10 +02:00
|
|
|
import javax.persistence.Query;
|
2011-03-22 23:02:06 +01:00
|
|
|
|
|
|
|
import de.hsadmin.core.model.AbstractEntity;
|
|
|
|
import de.hsadmin.core.model.AbstractModuleImpl;
|
|
|
|
import de.hsadmin.core.model.AuthorisationException;
|
|
|
|
import de.hsadmin.core.model.HSAdminException;
|
2011-08-12 16:19:21 +02:00
|
|
|
import de.hsadmin.core.model.Transaction;
|
2011-05-20 16:57:10 +02:00
|
|
|
import de.hsadmin.mods.pac.Pac;
|
2011-03-22 23:02:06 +01:00
|
|
|
import de.hsadmin.mods.user.UnixUser;
|
|
|
|
|
|
|
|
public class PgSqlDatabaseModuleImpl extends AbstractModuleImpl {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<AbstractEntity> search(Class<? extends AbstractEntity> entityClass, String condition, String orderBy) throws HSAdminException {
|
|
|
|
if (orderBy == null || orderBy.length() == 0) {
|
2011-04-01 18:53:37 +02:00
|
|
|
orderBy = "ORDER BY obj.name ASC";
|
2011-03-22 23:02:06 +01:00
|
|
|
}
|
|
|
|
return super.search(entityClass, condition, orderBy);
|
|
|
|
}
|
|
|
|
|
2011-05-20 16:25:45 +02:00
|
|
|
@Override
|
|
|
|
public AbstractEntity add(AbstractEntity newEntity) throws HSAdminException {
|
2011-08-12 16:19:21 +02:00
|
|
|
UnixUser loginUser = getTransaction().getLoginUser();
|
2011-05-20 16:25:45 +02:00
|
|
|
PgSqlDatabase database = (PgSqlDatabase) newEntity;
|
|
|
|
String name = database.getName();
|
2011-10-28 12:49:31 +02:00
|
|
|
String pacPrefix = name.substring(0, 5);
|
2011-05-20 16:57:10 +02:00
|
|
|
if (name.length() < 7 || name.charAt(5) != '_') {
|
2011-08-12 16:19:21 +02:00
|
|
|
throw new AuthorisationException(loginUser, "add", newEntity);
|
2011-05-20 16:25:45 +02:00
|
|
|
}
|
2011-10-28 12:49:31 +02:00
|
|
|
String owner = database.getOwner();
|
|
|
|
if (owner == null || name.length() < 7 || name.charAt(5) != '_') {
|
|
|
|
if (name.length() != 5) {
|
|
|
|
throw new HSAdminException("database owner required");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!owner.startsWith(pacPrefix)) {
|
|
|
|
throw new HSAdminException("wrong database owner");
|
|
|
|
}
|
2011-05-20 16:57:10 +02:00
|
|
|
EntityManager em = getTransaction().getEntityManager();
|
|
|
|
Query qPac = em.createQuery("SELECT obj FROM Pacs obj WHERE obj.name = :pacName");
|
|
|
|
qPac.setParameter("pacName", name.substring(0, 5));
|
|
|
|
Object singleResult = qPac.getSingleResult();
|
|
|
|
Pac pac = (Pac) singleResult;
|
2011-08-12 16:19:21 +02:00
|
|
|
if (pac == null || !pac.isReadAllowedFor(loginUser)) {
|
|
|
|
throw new AuthorisationException(loginUser, "add", newEntity);
|
2011-05-20 16:25:45 +02:00
|
|
|
}
|
2011-10-28 12:49:31 +02:00
|
|
|
if (!pac.getName().equals(pacPrefix)) {
|
|
|
|
throw new HSAdminException("wrong database owner");
|
|
|
|
}
|
2013-01-21 13:41:01 +01:00
|
|
|
database.setPac(pac);
|
2011-05-20 16:25:45 +02:00
|
|
|
return super.add(newEntity);
|
|
|
|
}
|
|
|
|
|
2011-03-22 23:02:06 +01:00
|
|
|
@Override
|
|
|
|
public AbstractEntity update(AbstractEntity existingEntity) throws HSAdminException {
|
2011-08-12 16:19:21 +02:00
|
|
|
Transaction transaction = getTransaction();
|
|
|
|
EntityManager em = transaction.getEntityManager();
|
|
|
|
UnixUser unixUser = transaction.getLoginUser();
|
2011-03-22 23:02:06 +01:00
|
|
|
MySqlDatabase detachtedDB = (MySqlDatabase) existingEntity;
|
|
|
|
MySqlDatabase attachedDB = em.find(MySqlDatabase.class, detachtedDB.getId());
|
|
|
|
if (!attachedDB.getName().equals(detachtedDB.getName())) {
|
|
|
|
throw new AuthorisationException(unixUser, "update", existingEntity, "name");
|
|
|
|
}
|
|
|
|
if (!attachedDB.getEncoding().equals(detachtedDB.getEncoding())) {
|
|
|
|
throw new AuthorisationException(unixUser, "update", existingEntity, "encoding");
|
|
|
|
}
|
|
|
|
if (!attachedDB.getInstance().equals(detachtedDB.getInstance())) {
|
|
|
|
throw new AuthorisationException(unixUser, "update", existingEntity, "instance");
|
|
|
|
}
|
|
|
|
return super.update(existingEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|