/* * Created on 11/Mar/2003 by jpvl * */ package net.sourceforge.fenixedu.domain; import java.util.Collection; import java.util.Comparator; import net.sourceforge.fenixedu._development.LogLevel; import net.sourceforge.fenixedu._development.PropertiesManager; import net.sourceforge.fenixedu.domain.audit.AuditOperation; import net.sourceforge.fenixedu.domain.exceptions.DomainException; import net.sourceforge.fenixedu.injectionCode.AccessControl; import org.joda.time.DateTime; import pt.ist.fenixframework.audit.IAuditor; import pt.ist.fenixframework.audit.Operation; import pt.ist.fenixframework.pstm.Transaction; import pt.utl.ist.fenix.tools.util.StringAppender; import com.linkare.commons.metainfo.Linkare; /** * @author jpvl */ public abstract class DomainObject extends DomainObject_Base { static final public Comparator COMPARATOR_BY_ID = new Comparator() { public int compare(DomainObject o1, DomainObject o2) { return o1.getIdInternal().compareTo(o2.getIdInternal()); } }; private static final boolean ERROR_IF_DELETED_OBJECT_NOT_DISCONNECTED = PropertiesManager .getBooleanProperty("error.if.deleted.object.not.disconnected"); public DomainObject() { super(); } @Override protected final void ensureIdInternal() { try { super.ensureIdInternal(); } catch (UnableToDetermineIdException t) { if (LogLevel.WARN) { System.out.println("Something went wrong when initializing the idInternal. Not setting it..."); } throw t; } } public boolean isDeleted() { return getRootDomainObject() == null; } protected abstract RootDomainObject getRootDomainObject(); protected final void deleteDomainObject() { if (!checkDisconnected()) { if (ERROR_IF_DELETED_OBJECT_NOT_DISCONNECTED) { throw new Error("Trying to delete a DomainObject that is still connected to other objects: " + this); } else { System.err.println("WARNING: Deleting a DomainObject that is still connected to other objects: " + this); } } Transaction.deleteObject(this); } protected String getCurrentUser() { if (AccessControl.getUserView() != null) { return AccessControl.getUserView().getUtilizador(); } else { return System.getProperty("user.name", "FENIX"); } } @Override public final String toString() { return StringAppender.append(getClass().getName(), "(", getIdInternal().toString(), ")"); } /** * This method allows you to obtains the reification of this object's type. * Note that the corresponding reification may no yet exist and, thus, this * method may return null. * * @return the {@link MetaDomainObject} that represents this object's type. */ public MetaDomainObject getMeta() { return MetaDomainObject.getMeta(getClass()); } protected void check(final Object obj, final String message, final String... args) { if (obj == null) { throw new DomainException(message, args); } } protected void check(final String obj, final String message, final String... args) { if (obj == null || obj.isEmpty()) { throw new DomainException(message, args); } } protected void check(final Object[] obj, final String message, final String... args) { if (obj == null || obj.length == 0) { throw new DomainException(message, args); } } protected void check(final Collection obj, final String message, final String... args) { if (obj == null || obj.size() == 0) { throw new DomainException(message, args); } } public final String getExternalId() { return String.valueOf(getOID()); } public static T fromExternalId(String extId) { if (extId == null) { return null; } else { return (T) Transaction.getObjectForOID(Long.parseLong(extId)); } } @Linkare(author = "Paulo Zenida") @Override public void createAuditOperation(IAuditor auditor, Operation operation) { if (shouldCreateAuditOperation(operation)) { new AuditOperation(auditor, this, operation, new DateTime()); } } private boolean shouldCreateAuditOperation(final Operation operation) { switch (operation) { case CREATE: return PropertiesManager.getBooleanProperty("audit.record.create"); case UPDATE: return PropertiesManager.getBooleanProperty("audit.record.update"); case REMOVE: return PropertiesManager.getBooleanProperty("audit.record.remove"); } return false; } }