raimund@833: package de.intevation.lada.util.auth; raimund@833: raimund@833: import java.util.List; raimund@833: raimund@833: import javax.inject.Inject; raimund@833: import javax.persistence.EntityManager; raimund@833: raimund@833: import de.intevation.lada.model.land.LMessung; raimund@833: import de.intevation.lada.model.land.LProbe; raimund@833: import de.intevation.lada.model.land.LStatusProtokoll; raimund@833: import de.intevation.lada.util.annotation.RepositoryConfig; raimund@833: import de.intevation.lada.util.data.QueryBuilder; raimund@833: import de.intevation.lada.util.data.Repository; raimund@833: import de.intevation.lada.util.data.RepositoryType; raimund@833: import de.intevation.lada.util.rest.Response; raimund@833: raimund@833: public abstract class BaseAuthorizer implements Authorizer { raimund@833: raimund@833: /** raimund@833: * The Repository used to read from Database. raimund@833: */ raimund@833: @Inject raimund@833: @RepositoryConfig(type=RepositoryType.RO) raimund@833: protected Repository repository; raimund@833: raimund@833: /** raimund@833: * Get the authorization of a single probe. raimund@833: * raimund@833: * @param userInfo The user information. raimund@833: * @param probe The probe to authorize. raimund@833: */ raimund@833: protected boolean getAuthorization(UserInfo userInfo, LProbe probe) { raimund@833: if (userInfo.getMessstellen().contains(probe.getMstId())) { raimund@833: return true; raimund@833: } raimund@833: else { raimund@833: return false; raimund@833: } raimund@833: } raimund@833: raimund@833: /** raimund@833: * Test whether a probe is readonly. raimund@833: * raimund@833: * @param probeId The probe Id. raimund@833: * @return True if the probe is readonly. raimund@833: */ raimund@833: public boolean isProbeReadOnly(Integer probeId) { raimund@833: EntityManager manager = repository.entityManager("land"); raimund@833: QueryBuilder builder = raimund@833: new QueryBuilder( raimund@833: manager, raimund@833: LMessung.class); raimund@833: builder.and("probeId", probeId); raimund@833: Response response = repository.filter(builder.getQuery(), "land"); raimund@833: @SuppressWarnings("unchecked") raimund@833: List messungen = (List) response.getData(); raimund@833: for (int i = 0; i < messungen.size(); i++) { raimund@833: if (messungen.get(i).getStatus() == null) { raimund@840: continue; raimund@833: } raimund@833: LStatusProtokoll status = repository.getByIdPlain( raimund@833: LStatusProtokoll.class, messungen.get(i).getStatus(), "land"); raimund@833: if (status.getStatusWert() != 0 && status.getStatusWert() != 4) { raimund@833: return true; raimund@833: } raimund@833: } raimund@833: return false; raimund@833: } raimund@833: raimund@833: public boolean isMessungReadOnly(Integer messungsId) { raimund@833: LMessung messung = raimund@833: repository.getByIdPlain(LMessung.class, messungsId, "land"); raimund@833: if (messung.getStatus() == null) { raimund@833: return false; raimund@833: } raimund@833: LStatusProtokoll status = repository.getByIdPlain( raimund@833: LStatusProtokoll.class, raimund@833: messung.getStatus(), raimund@833: "land"); raimund@833: return (status.getStatusWert() != 0 && status.getStatusWert() != 4); raimund@833: } raimund@833: raimund@833: }