package net.sourceforge.fenixedu.dataTransferObject.protocol; import java.io.InputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; import net.sourceforge.fenixedu.domain.Country; import net.sourceforge.fenixedu.domain.DomainListReference; import net.sourceforge.fenixedu.domain.DomainReference; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.Teacher; import net.sourceforge.fenixedu.domain.organizationalStructure.Function; import net.sourceforge.fenixedu.domain.organizationalStructure.Unit; import net.sourceforge.fenixedu.domain.organizationalStructure.UnitName; import net.sourceforge.fenixedu.domain.person.PersonName; import net.sourceforge.fenixedu.domain.protocols.Protocol; import net.sourceforge.fenixedu.domain.protocols.ProtocolFile; import net.sourceforge.fenixedu.domain.protocols.ProtocolHistory; import net.sourceforge.fenixedu.domain.protocols.util.ProtocolAction; import net.sourceforge.fenixedu.domain.protocols.util.ProtocolActionType; import net.sourceforge.fenixedu.domain.util.FactoryExecutor; import org.joda.time.YearMonthDay; public class ProtocolFactory implements Serializable, FactoryExecutor { public static enum EditProtocolAction { EDIT_PROTOCOL_DATA, ADD_RESPONSIBLE, REMOVE_RESPONSIBLE, ADD_UNIT, REMOVE_UNIT, DELETE_FILE } public static enum FilePermissionType { IST_PEOPLE, RESPONSIBLES_AND_SCIENTIFIC_COUNCIL } private EditProtocolAction editProtocolAction; private DomainReference protocol; private String protocolNumber; private YearMonthDay signedDate; private Boolean renewable; private Boolean active; private String scientificAreas; private String observations; private ProtocolAction protocolAction; private YearMonthDay beginDate; private YearMonthDay endDate; private String otherActionTypes; private Boolean istResponsible; private Boolean internalUnit; private Boolean istResponsibleIsPerson; private Boolean functionByPerson; private DomainReference responsible; private DomainReference responsibleFunction; private DomainReference partnerResponsible; private String responsibleName; private String responsibleFunctionName; private DomainReference unitObject; private String unitName; private DomainReference responsibleToAdd; private DomainReference responsibleToRemove; private DomainReference responsibleFunctionToRemove; private DomainReference unitToAdd; private DomainReference unitToRemove; private List actionTypes; private DomainListReference responsibles; private DomainListReference responsibleFunctions; private DomainReference responsibleFunctionUnit; private DomainListReference partnerResponsibles; private DomainListReference partnerUnits; private DomainListReference units; private DomainListReference protocolFiles; private DomainReference fileToDelete; private DomainListReference protocolHistories; private transient InputStream inputStream; private String fileName; private FilePermissionType filePermissionType; private List fileBeans; private DomainReference country; public ProtocolFactory(Protocol protocol) { setProtocol(protocol); setProtocolNumber(protocol.getProtocolNumber()); setSignedDate(protocol.getSignedDate()); setObservations(protocol.getObservations()); setRenewable(protocol.getRenewable()); setActive(protocol.getActive()); setProtocolHistories(protocol.getProtocolHistories()); setProtocolAction(protocol.getProtocolAction()); setOtherActionTypes(protocol.getProtocolAction().getOtherTypes()); setActionTypes(protocol.getProtocolAction().getProtocolActionTypes()); setScientificAreas(protocol.getScientificAreas()); setResponsiblesList(protocol.getResponsibles()); setResponsibleFunctions(protocol.getResponsibleFunctions()); setPartnerResponsiblesList(protocol.getPartnerResponsibles()); setUnitsList(protocol.getUnits()); setPartnersList(protocol.getPartners()); setIstResponsible(true); setInternalUnit(true); setIstResponsibleIsPerson(true); setFunctionByPerson(true); setFilePermissionType(FilePermissionType.RESPONSIBLES_AND_SCIENTIFIC_COUNCIL); } private void setPartnersList(List partners) { if (partners != null) { setPartnerUnits(new DomainListReference(partners)); } } private void setUnitsList(List units) { if (units != null) { setUnits(new DomainListReference(units)); } } private void setPartnerResponsiblesList(List partnerResponsibles) { if (partnerResponsibles != null) { setPartnerResponsibles(new DomainListReference(partnerResponsibles)); } } private void setResponsiblesList(List responsibles) { if (responsibles != null) { setResponsibles(new DomainListReference(responsibles)); } } private void setActionTypes(EnumSet protocolActionTypes) { if (protocolActionTypes != null) { ArrayList actionsTypesList = new ArrayList(); for (ProtocolActionType actionType : protocolActionTypes) { actionsTypesList.add(actionType); } setActionTypes(actionsTypesList); } } public ProtocolFactory() { setIstResponsible(true); setIstResponsibleIsPerson(true); setFunctionByPerson(true); setInternalUnit(true); setFilePermissionType(FilePermissionType.RESPONSIBLES_AND_SCIENTIFIC_COUNCIL); } public Object execute() { if (getProtocol() == null) { return new Protocol(this); } else { switch (getEditProtocolAction()) { case EDIT_PROTOCOL_DATA: getProtocol().editData(this); break; case ADD_RESPONSIBLE: getProtocol().addResponsible(this); break; case REMOVE_RESPONSIBLE: getProtocol().removeResponsible(this); break; case ADD_UNIT: getProtocol().addUnit(this); break; case REMOVE_UNIT: getProtocol().removeUnit(this); break; case DELETE_FILE: getProtocol().deleteFile(this); break; default: return null; } return getProtocol(); } } public Boolean getActive() { return active; } public void setActive(Boolean active) { this.active = active; } public String getObservations() { return observations; } public void setObservations(String observations) { this.observations = observations; } public List getPartnerResponsibles() { return partnerResponsibles; } public void setPartnerResponsibles(List partnerResponsibles) { this.partnerResponsibles = new DomainListReference(partnerResponsibles); } public DomainListReference getPartnerUnits() { return partnerUnits; } public void setPartnerUnits(DomainListReference partnerUnits) { this.partnerUnits = partnerUnits; } public Unit getResponsibleFunctionUnit() { return responsibleFunctionUnit == null ? null : responsibleFunctionUnit.getObject(); } public void setResponsibleFunctionUnit(Unit responsibleFunctionUnit) { this.responsibleFunctionUnit = new DomainReference(responsibleFunctionUnit); } public Protocol getProtocol() { return protocol != null ? protocol.getObject() : null; } public void setProtocol(Protocol protocol) { if (protocol != null) { this.protocol = new DomainReference(protocol); } else { this.protocol = null; } } public ProtocolAction getProtocolAction() { return protocolAction; } public void setProtocolAction(ProtocolAction protocolAction) { this.protocolAction = protocolAction; } public String getProtocolNumber() { return protocolNumber; } public void setProtocolNumber(String protocolNumber) { this.protocolNumber = protocolNumber; } public Boolean getRenewable() { return renewable; } public void setRenewable(Boolean renewable) { this.renewable = renewable; } public List getResponsibles() { return responsibles; } public void setResponsibles(List responsibles) { this.responsibles = new DomainListReference(responsibles); } public List getResponsibleFunctions() { return responsibleFunctions; } public void setResponsibleFunctions(List responsibleFunctions) { this.responsibleFunctions = new DomainListReference(responsibleFunctions); } public String getScientificAreas() { return scientificAreas; } public void setScientificAreas(String scientificAreas) { this.scientificAreas = scientificAreas; } public YearMonthDay getSignedDate() { return signedDate; } public void setSignedDate(YearMonthDay signedDate) { this.signedDate = signedDate; } public DomainListReference getUnits() { return units; } public void setUnits(DomainListReference units) { this.units = units; } public YearMonthDay getBeginDate() { return beginDate; } public void setBeginDate(YearMonthDay beginDate) { this.beginDate = beginDate; } public YearMonthDay getEndDate() { return endDate; } public void setEndDate(YearMonthDay endDate) { this.endDate = endDate; } public List getActionTypes() { return actionTypes; } public void setActionTypes(List actionTypes) { this.actionTypes = actionTypes; } public String getOtherActionTypes() { return otherActionTypes; } public void setOtherActionTypes(String otherActionTypes) { this.otherActionTypes = otherActionTypes; } public DomainListReference getProtocolFiles() { return protocolFiles; } public void setProtocolFiles(DomainListReference protocolFiles) { this.protocolFiles = protocolFiles; } public EditProtocolAction getEditProtocolAction() { return editProtocolAction; } public void setEditProtocolAction(EditProtocolAction editProtocolAction) { this.editProtocolAction = editProtocolAction; } public Boolean getIstResponsible() { return istResponsible; } public void setIstResponsible(Boolean istResponsible) { this.istResponsible = istResponsible; } public PersonName getPartnerResponsible() { return partnerResponsible != null ? partnerResponsible.getObject() : null; } public void setPartnerResponsible(PersonName responsible) { this.partnerResponsible = (responsible != null) ? new DomainReference(responsible) : null; } public Teacher getResponsible() { return responsible != null ? responsible.getObject() : null; } public void setResponsible(Teacher responsible) { this.responsible = (responsible != null) ? new DomainReference(responsible) : null; } public Function getResponsibleFunction() { return responsibleFunction != null ? responsibleFunction.getObject() : null; } public void setResponsibleFunction(Function responsibleFunction) { this.responsibleFunction = (responsibleFunction != null) ? new DomainReference(responsibleFunction) : null; } public UnitName getUnitObject() { return unitObject != null ? unitObject.getObject() : null; } public void setUnitObject(UnitName unitObject) { this.unitObject = unitObject != null ? new DomainReference(unitObject) : null; } public Boolean getInternalUnit() { return internalUnit; } public void setInternalUnit(Boolean internalUnit) { this.internalUnit = internalUnit; } public String getResponsibleName() { return responsibleName; } public void setResponsibleName(String responsibleName) { this.responsibleName = responsibleName; } public String getUnitName() { return unitName; } public void setUnitName(String unitName) { this.unitName = unitName; } public boolean addISTResponsible() { if (getResponsibles() == null) { setResponsibles(new DomainListReference()); } if (getResponsibles().contains(getResponsible().getPerson())) { return false; } else { return getResponsibles().add(getResponsible().getPerson()); } } public boolean addISTResponsibleFunction() { if (getResponsibleFunctions() == null) { setResponsibleFunctions(new DomainListReference()); } if (getResponsibleFunctions().contains(getResponsibleFunction())) { return false; } else { return getResponsibleFunctions().add(getResponsibleFunction()); } } public boolean addPartnerResponsible() { if (getPartnerResponsibles() == null) { setPartnerResponsibles(new DomainListReference()); } if (getPartnerResponsibles().contains(getPartnerResponsible().getPerson())) { return false; } else { return getPartnerResponsibles().add(getPartnerResponsible().getPerson()); } } public void addPartnerResponsible(Person responsible) { if (getPartnerResponsibles() == null) { setPartnerResponsibles(new DomainListReference()); } getPartnerResponsibles().add(responsible); } public boolean addISTUnit() { if (getUnits() == null) { setUnits(new DomainListReference()); } if (getUnits().contains(getUnitObject().getUnit())) { return false; } else { return getUnits().add(getUnitObject().getUnit()); } } public boolean addPartnerUnit() { if (getPartnerUnits() == null) { setPartnerUnits(new DomainListReference()); } if (getPartnerUnits().contains(getUnitObject().getUnit())) { return false; } else { return getPartnerUnits().add(getUnitObject().getUnit()); } } public void addISTUnit(Unit unit) { if (getUnits() == null) { setUnits(new DomainListReference()); } getUnits().add(unit); } public void addPartnerUnit(Unit unit) { if (getPartnerUnits() == null) { setPartnerUnits(new DomainListReference()); } getPartnerUnits().add(unit); } public void addFile() { if (getFileBeans() == null) { setFileBeans(new ArrayList()); } getFileBeans().add(new ProtocolFileBean(getInputStream(), getFileName(), getFilePermissionType())); } public void removeFile(String fileName) { for (ProtocolFileBean protocolFileBean : getFileBeans()) { if (protocolFileBean.getFileName().equals(fileName)) { getFileBeans().remove(protocolFileBean); break; } } } public void resetSearches() { setPartnerResponsible(null); setResponsible(null); setResponsibleName(null); setUnitObject(null); setUnitName(null); } public void resetFile() { setFileName(null); setInputStream(null); } public ProtocolHistory getActualProtocolHistory() { return getProtocol().getActualProtocolHistory(); } public Person getResponsibleToAdd() { return responsibleToAdd != null ? responsibleToAdd.getObject() : null; } public void setResponsibleToAdd(Person responsibleToAdd) { this.responsibleToAdd = responsibleToAdd != null ? new DomainReference(responsibleToAdd) : null; } public Person getResponsibleToRemove() { return responsibleToRemove != null ? responsibleToRemove.getObject() : null; } public void setResponsibleToRemove(Person responsibleToRemove) { this.responsibleToRemove = responsibleToRemove != null ? new DomainReference(responsibleToRemove) : null; } public Function getResponsibleFunctionToRemove() { return responsibleFunctionToRemove != null ? responsibleFunctionToRemove.getObject() : null; } public void setResponsibleFunctionToRemove(Function responsibleFunctionToRemove) { this.responsibleFunctionToRemove = responsibleFunctionToRemove != null ? new DomainReference( responsibleFunctionToRemove) : null; } public Unit getUnitToAdd() { return unitToAdd != null ? unitToAdd.getObject() : null; } public void setUnitToAdd(Unit unitToAdd) { this.unitToAdd = unitToAdd != null ? new DomainReference(unitToAdd) : null; } public Unit getUnitToRemove() { return unitToRemove != null ? unitToRemove.getObject() : null; } public void setUnitToRemove(Unit unitToRemove) { this.unitToRemove = unitToRemove != null ? new DomainReference(unitToRemove) : null; } public ProtocolFile getFileToDelete() { return fileToDelete != null ? fileToDelete.getObject() : null; } public void setFileToDelete(ProtocolFile fileToDelete) { this.fileToDelete = fileToDelete != null ? new DomainReference(fileToDelete) : null; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public FilePermissionType getFilePermissionType() { return filePermissionType; } public void setFilePermissionType(FilePermissionType filePermissionType) { this.filePermissionType = filePermissionType; } public List getFileBeans() { return fileBeans; } public void setFileBeans(List fileBeans) { this.fileBeans = fileBeans; } public Country getCountry() { return country != null ? country.getObject() : null; } public void setCountry(Country country) { this.country = country != null ? new DomainReference(country) : null; } public List getProtocolHistories() { return protocolHistories; } public void setProtocolHistories(List protocolHistories) { this.protocolHistories = new DomainListReference(protocolHistories); } public Boolean getIstResponsibleIsPerson() { return istResponsibleIsPerson; } public void setIstResponsibleIsPerson(Boolean istResponsibleIsPerson) { this.istResponsibleIsPerson = istResponsibleIsPerson; } public String getResponsibleFunctionName() { return responsibleFunctionName; } public void setResponsibleFunctionName(String responsibleFunctionName) { this.responsibleFunctionName = responsibleFunctionName; } public Boolean getFunctionByPerson() { return functionByPerson; } public void setFunctionByPerson(Boolean functionByPerson) { this.functionByPerson = functionByPerson; } }