// Path: dom/src/main/java/com/ofbizian/semat/dom/domain/AbstractPersistable.java
// @javax.jdo.annotations.PersistenceCapable(
//         identityType= IdentityType.DATASTORE,
//         schema = "simple"
// )
// @javax.jdo.annotations.Inheritance(strategy=javax.jdo.annotations.InheritanceStrategy.SUBCLASS_TABLE)
// @javax.jdo.annotations.DatastoreIdentity(
//         strategy=javax.jdo.annotations.IdGeneratorStrategy.IDENTITY,
//         column="id")
// 
// @javax.jdo.annotations.Version(
//         strategy= VersionStrategy.DATE_TIME,
//         column="version")
// 
// @DomainObject(
//         publishing = Publishing.ENABLED,
//         auditing = Auditing.ENABLED
// )
// public abstract class AbstractPersistable implements Serializable, Comparable<AbstractPersistable> {
// 
//      private String applicationTenancyPath;
// 
//     @javax.jdo.annotations.Column(
//             length = ApplicationTenancy.MAX_LENGTH_PATH,
//             allowsNull = "false",
//             name = "atPath"
//     )
//     @org.apache.isis.applib.annotation.Property(hidden = Where.EVERYWHERE)
//     public String getApplicationTenancyPath() {
//         return applicationTenancyPath;
//     }
// 
//     public void setApplicationTenancyPath(final String applicationTenancyPath) {
//         this.applicationTenancyPath = applicationTenancyPath;
//     }
// 
//     @PropertyLayout(
//             hidden = Where.ALL_TABLES,
//             describedAs = "Determines those users for whom this object is available to view and/or modify."
//     )
// 
//     @Programmatic
//     public ApplicationTenancy getApplicationTenancy() {
//         final ApplicationTenancy applicationTenancy = applicationTenancyRepository.findByPathCached(applicationTenancyPath);
//         if (applicationTenancy == null) {
//             throw new FatalException("Domain Object without Application Tenancy.");
//         }
//         return applicationTenancy;
//     }
// 
//     public int compareTo(AbstractPersistable other) {
//         throw new RuntimeException("compareTo not implemtned for: " + this.getClass());
// //        return new CompareToBuilder()
// //                .append(getClass().getName(), other.getClass().getName())
// //                .toComparison();
//     }
// 
//     //region > id (programmatic, for comparison)
//     @Programmatic
//     public String getId() {
//         Object objectId = JDOHelper.getObjectId(this);
//         if (objectId == null) {
//             return "";
//         }
//         String objectIdStr = objectId.toString();
//         final String id = objectIdStr.split("\\[OID\\]")[0];
//         return id;
//     }
//     //endregion
// 
// 
// 
//     public void init() {
//         if (applicationTenancyPath == null) {
//             final String username = userService.getUser().getName();
//             final ApplicationUser applicationUser = applicationUserRepository.findByUsername(username);
//             String atPath = applicationUser.getAtPath();
//             final ApplicationTenancy applicationTenancy = applicationTenancyRepository.findByPathCached(atPath);
//             if(applicationTenancy == null) {
//                 throw new IllegalStateException(String.format("No application tenancy defined for user '%s'", username));
//             }
//             applicationTenancyPath = applicationTenancy.getPath();
//         } else {
//             throw new IllegalStateException(String.format("Application tenancy defined for object '%s'", this));
//         }
//     }
// 
//     @Override
//     public boolean equals(final Object obj) {
//         if (obj == null) {
//             return false;
//         }
//         if (obj == this) {
//             return true;
//         }
//         if (!(obj instanceof AbstractPersistable)) {
//             return false;
//         }
//         AbstractPersistable rhs = (AbstractPersistable) obj;
//         return new EqualsBuilder().
//                 append(getId(), rhs.getId()).
//                 isEquals();
//     }
// 
//     @Override
//     public int hashCode() {
//         return new HashCodeBuilder().append(getId()).hashCode();
//     }
// 
// 
//     public String toString() {
//         return getClass().getName().replaceAll(".*\\.", "");
//     }
// 
//     @Inject
//     private ApplicationTenancyRepository applicationTenancyRepository;
// 
//     @javax.inject.Inject
//     private ApplicationUserRepository applicationUserRepository;
// 
//     @javax.inject.Inject
//     private UserService userService;
// 
// }


import javax.inject.Inject;
import com.ofbizian.semat.dom.domain.AbstractPersistable;
import org.apache.isis.applib.annotation.DomainService;
import org.apache.isis.applib.annotation.NatureOfService;
import org.isisaddons.module.security.dom.tenancy.ApplicationTenancy;
import org.isisaddons.module.security.dom.tenancy.ApplicationTenancyEvaluator;
import org.isisaddons.module.security.dom.tenancy.ApplicationTenancyRepository;
import org.isisaddons.module.security.dom.user.ApplicationUser;
package com.ofbizian.semat.app.services.tenancyevaluator;



@DomainService(nature = NatureOfService.DOMAIN)
public class SematApplicationTenancyEvaluator implements ApplicationTenancyEvaluator {

    @Override
    public boolean handles(Class<?> cls) {
        return AbstractPersistable.class.isAssignableFrom(cls);
    }

    @Override
    public String hides(Object domainObject, ApplicationUser applicationUser) {
        if (!(domainObject instanceof AbstractPersistable)) {
            return null;
        }

// Path: dom/src/main/java/com/ofbizian/semat/dom/domain/AbstractPersistable.java
// @javax.jdo.annotations.PersistenceCapable(
//         identityType= IdentityType.DATASTORE,
//         schema = "simple"
// )
// @javax.jdo.annotations.Inheritance(strategy=javax.jdo.annotations.InheritanceStrategy.SUBCLASS_TABLE)
// @javax.jdo.annotations.DatastoreIdentity(
//         strategy=javax.jdo.annotations.IdGeneratorStrategy.IDENTITY,
//         column="id")
// 
// @javax.jdo.annotations.Version(
//         strategy= VersionStrategy.DATE_TIME,
//         column="version")
// 
// @DomainObject(
//         publishing = Publishing.ENABLED,
//         auditing = Auditing.ENABLED
// )
// public abstract class AbstractPersistable implements Serializable, Comparable<AbstractPersistable> {
// 
//      private String applicationTenancyPath;
// 
//     @javax.jdo.annotations.Column(
//             length = ApplicationTenancy.MAX_LENGTH_PATH,
//             allowsNull = "false",
//             name = "atPath"
//     )
//     @org.apache.isis.applib.annotation.Property(hidden = Where.EVERYWHERE)
//     public String getApplicationTenancyPath() {
//         return applicationTenancyPath;
//     }
// 
//     public void setApplicationTenancyPath(final String applicationTenancyPath) {
//         this.applicationTenancyPath = applicationTenancyPath;
//     }
// 
//     @PropertyLayout(
//             hidden = Where.ALL_TABLES,
//             describedAs = "Determines those users for whom this object is available to view and/or modify."
//     )
// 
//     @Programmatic
//     public ApplicationTenancy getApplicationTenancy() {
//         final ApplicationTenancy applicationTenancy = applicationTenancyRepository.findByPathCached(applicationTenancyPath);
//         if (applicationTenancy == null) {
//             throw new FatalException("Domain Object without Application Tenancy.");
//         }
//         return applicationTenancy;
//     }
// 
//     public int compareTo(AbstractPersistable other) {
//         throw new RuntimeException("compareTo not implemtned for: " + this.getClass());
// //        return new CompareToBuilder()
// //                .append(getClass().getName(), other.getClass().getName())
// //                .toComparison();
//     }
// 
//     //region > id (programmatic, for comparison)
//     @Programmatic
//     public String getId() {
//         Object objectId = JDOHelper.getObjectId(this);
//         if (objectId == null) {
//             return "";
//         }
//         String objectIdStr = objectId.toString();
//         final String id = objectIdStr.split("\\[OID\\]")[0];
//         return id;
//     }
//     //endregion
// 
// 
// 
//     public void init() {
//         if (applicationTenancyPath == null) {
//             final String username = userService.getUser().getName();
//             final ApplicationUser applicationUser = applicationUserRepository.findByUsername(username);
//             String atPath = applicationUser.getAtPath();
//             final ApplicationTenancy applicationTenancy = applicationTenancyRepository.findByPathCached(atPath);
//             if(applicationTenancy == null) {
//                 throw new IllegalStateException(String.format("No application tenancy defined for user '%s'", username));
//             }
//             applicationTenancyPath = applicationTenancy.getPath();
//         } else {
//             throw new IllegalStateException(String.format("Application tenancy defined for object '%s'", this));
//         }
//     }
// 
//     @Override
//     public boolean equals(final Object obj) {
//         if (obj == null) {
//             return false;
//         }
//         if (obj == this) {
//             return true;
//         }
//         if (!(obj instanceof AbstractPersistable)) {
//             return false;
//         }
//         AbstractPersistable rhs = (AbstractPersistable) obj;
//         return new EqualsBuilder().
//                 append(getId(), rhs.getId()).
//                 isEquals();
//     }
// 
//     @Override
//     public int hashCode() {
//         return new HashCodeBuilder().append(getId()).hashCode();
//     }
// 
// 
//     public String toString() {
//         return getClass().getName().replaceAll(".*\\.", "");
//     }
// 
//     @Inject
//     private ApplicationTenancyRepository applicationTenancyRepository;
// 
//     @javax.inject.Inject
//     private ApplicationUserRepository applicationUserRepository;
// 
//     @javax.inject.Inject
//     private UserService userService;
// 
// }


// Path: app/src/main/java/com/ofbizian/semat/app/services/tenancyevaluator/SematApplicationTenancyEvaluator.java
import javax.inject.Inject;
import com.ofbizian.semat.dom.domain.AbstractPersistable;
import org.apache.isis.applib.annotation.DomainService;
import org.apache.isis.applib.annotation.NatureOfService;
import org.isisaddons.module.security.dom.tenancy.ApplicationTenancy;
import org.isisaddons.module.security.dom.tenancy.ApplicationTenancyEvaluator;
import org.isisaddons.module.security.dom.tenancy.ApplicationTenancyRepository;
import org.isisaddons.module.security.dom.user.ApplicationUser;

package com.ofbizian.semat.app.services.tenancyevaluator;



@DomainService(nature = NatureOfService.DOMAIN)
public class SematApplicationTenancyEvaluator implements ApplicationTenancyEvaluator {

    @Override
    public boolean handles(Class<?> cls) {
        return AbstractPersistable.class.isAssignableFrom(cls);
    }

    @Override
    public String hides(Object domainObject, ApplicationUser applicationUser) {
        if (!(domainObject instanceof AbstractPersistable)) {
            return null;
        }

        final AbstractPersistable persistable = (AbstractPersistable) domainObject;