package net.sourceforge.fenixedu.domain.softwareLicenseManagement; import java.util.Collections; import java.util.ResourceBundle; import java.util.Set; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.RootDomainObject; import net.sourceforge.fenixedu.util.LanguageUtils; import org.joda.time.LocalDate; import pt.ist.fenixWebFramework.rendererExtensions.util.IPresentableEnum; import pt.ist.fenixWebFramework.services.Service; import pt.ist.fenixframework.plugins.luceneIndexing.IndexableField; import pt.ist.fenixframework.plugins.luceneIndexing.domain.IndexDocument; import pt.ist.fenixframework.plugins.luceneIndexing.domain.interfaces.Indexable; import pt.ist.fenixframework.plugins.luceneIndexing.domain.interfaces.Searchable; public class LicenseRequest extends LicenseRequest_Base implements Searchable, Indexable { public static enum LicenseRequestIndexFields implements IndexableField { LOCK_CODE("lockCode"), VERSION_NUMBER("version"), APPLICATION("app"); private String name; private LicenseRequestIndexFields(String name) { this.name = name; } @Override public String getFieldName() { return name; } } public static enum LicenseRequestState implements IPresentableEnum { ACTIVE, PENDING, EXPIRED, CANCELLED; @Override public String getLocalizedName() { return ResourceBundle.getBundle("resources.PersonResources", LanguageUtils.getLocale()).getString( "label.LicenseRequestState." + name()); } } private LicenseRequest(SoftwareVersion version, Person requestor, String installCode) { super(); setInstallCode(installCode); setRequestedVersion(version); setRequestor(requestor); setActive(Boolean.TRUE); setRootDomainObject(RootDomainObject.getInstance()); } @Service public static LicenseRequest createNewRequest(SoftwareVersion version, Person requestor, String installCode) { final LicenseRequest licenseRequest = new LicenseRequest(version, requestor, installCode); version.getLicenseModel().process(licenseRequest); return licenseRequest; } public boolean isPending() { return super.getActive() && getBegin() == null && getEnd() == null; } public boolean isExpired() { LocalDate now = new LocalDate(); return Boolean.TRUE.equals(super.getActive()) && getEnd().isBefore(now); } @Override public Boolean getActive() { return super.getActive() && getState() == LicenseRequestState.ACTIVE; } public LicenseRequestState getState() { if (Boolean.FALSE.equals(super.getActive())) { return LicenseRequestState.CANCELLED; } else if (isPending()) { return LicenseRequestState.PENDING; } return isExpired() ? LicenseRequestState.EXPIRED : LicenseRequestState.ACTIVE; } public void deactivate() { super.setActive(Boolean.FALSE); } @Override public IndexDocument getDocumentToIndex() { IndexDocument document = new IndexDocument(this); document.indexField(LicenseRequestIndexFields.LOCK_CODE, getInstallCode()); document.indexField(LicenseRequestIndexFields.VERSION_NUMBER, getRequestedVersion().getVersionIdentifier()); document.indexField(LicenseRequestIndexFields.APPLICATION, getRequestedVersion().getApplication().getName()); return document; } @Override public Set getObjectsToIndex() { return (Set) (hasRootDomainObject() ? Collections.singleton((Indexable) this) : Collections.emptySet()); } }