Skip to content
Snippets Groups Projects

Rama5

Merged
ginquinrequested to merge
rama5 into master
102 files
+ 8711
1720
Compare changes
  • Side-by-side
  • Inline

Files

 
package es.uva.inf.tfg.ginquin.smellswisdom.controller.element;
 
 
import java.util.Comparator;
 
import java.util.HashMap;
 
import java.util.List;
 
import java.util.Map;
 
 
import org.zkoss.lang.Strings;
 
import org.zkoss.util.resource.Labels;
 
import org.zkoss.zk.ui.Component;
 
import org.zkoss.zk.ui.Executions;
 
import org.zkoss.zk.ui.event.Event;
 
import org.zkoss.zk.ui.event.EventQueue;
 
import org.zkoss.zk.ui.event.EventQueues;
 
import org.zkoss.zk.ui.select.SelectorComposer;
 
import org.zkoss.zk.ui.select.annotation.Listen;
 
import org.zkoss.zk.ui.select.annotation.VariableResolver;
 
import org.zkoss.zk.ui.select.annotation.Wire;
 
import org.zkoss.zk.ui.select.annotation.WireVariable;
 
import org.zkoss.zk.ui.util.Clients;
 
import org.zkoss.zkplus.spring.DelegatingVariableResolver;
 
import org.zkoss.zul.Checkbox;
 
import org.zkoss.zul.Combobox;
 
import org.zkoss.zul.ListModelList;
 
import org.zkoss.zul.ListModels;
 
import org.zkoss.zul.Textbox;
 
import org.zkoss.zul.Window;
 
 
import es.uva.inf.tfg.ginquin.smellswisdom.domain.Author;
 
import es.uva.inf.tfg.ginquin.smellswisdom.domain.Authorinstitution;
 
import es.uva.inf.tfg.ginquin.smellswisdom.domain.Institution;
 
import es.uva.inf.tfg.ginquin.smellswisdom.services.AuthorService;
 
import es.uva.inf.tfg.ginquin.smellswisdom.services.InstitutionService;
 
 
@VariableResolver(DelegatingVariableResolver.class)
 
public class AddAuthorshipController extends SelectorComposer<Component> {
 
 
/**
 
*
 
*/
 
private static final long serialVersionUID = 1L;
 
 
@Wire
 
Window modalDialog;
 
@Wire
 
Combobox surnameComboBox;
 
@Wire
 
Combobox nameComboBox;
 
@Wire
 
Textbox emailTextBox;
 
 
@Wire
 
Combobox acronymComboBox;
 
@Wire
 
Combobox fullnameComboBox;
 
@Wire
 
Textbox countryTextBox;
 
@Wire
 
Checkbox isAcademicCheckBox;
 
 
@WireVariable
 
private AuthorService authorService;
 
@WireVariable
 
private InstitutionService institutionService;
 
 
private ListModelList<Author> authorsListModel = null;
 
private ListModelList<Author> authorsListModel1 = null;
 
 
private ListModelList<Institution> institutionsListModel = null;
 
private ListModelList<Institution> institutionsListModel1 = null;
 
 
private Author selectedAuthor = null;
 
private Institution selectedInstitution = null;
 
 
// Evento para devolver el authorship
 
private EventQueue<Event> eq = null;
 
 
private String event = "";
 
private String param = "";
 
 
@SuppressWarnings("unchecked")
 
@Override
 
public void doAfterCompose(Component comp) throws Exception {
 
super.doAfterCompose(comp);
 
 
// Se recogen los parametros pasados en la ventana padre.
 
Map<String, Object> myParams = new HashMap<String, Object>();
 
myParams = (Map<String, Object>) Executions.getCurrent().getArg();
 
param = (String) myParams.get("param");
 
event = (String) myParams.get("event");
 
 
initAddAuthorship();
 
}
 
 
@Listen("onSelect=#surnameComboBox")
 
public void selectAuthor(Event e) {
 
if (authorsListModel.isSelectionEmpty()) {
 
selectedAuthor = null;
 
}else {
 
selectedAuthor = authorsListModel.getSelection().iterator().next();
 
}
 
refreshAuthorView();
 
}
 
 
@Listen("onSelect=#nameComboBox")
 
public void selectAuthor1(Event e) {
 
if (authorsListModel1.isSelectionEmpty()) {
 
selectedAuthor = authorsListModel1.getSelection().iterator().next();
 
}else {
 
selectedAuthor = null;
 
}
 
refreshAuthorView();
 
}
 
 
@Listen("onSelect=#acronymComboBox")
 
public void selectInstitution(Event e) {
 
if (institutionsListModel.isSelectionEmpty()) {
 
selectedInstitution = null;
 
}else {
 
selectedInstitution = institutionsListModel.getSelection().iterator().next();
 
}
 
refreshInstitutionView();
 
}
 
 
@Listen("onSelect=#fullnameComboBox")
 
public void selectInstitution1(Event e) {
 
if (institutionsListModel1.isSelectionEmpty()) {
 
selectedInstitution = null;
 
}else {
 
selectedInstitution = institutionsListModel1.getSelection().iterator().next();
 
}
 
refreshInstitutionView();
 
}
 
 
@Listen("onClick = #okButton")
 
public void saveAuthorShip(Event e) {
 
 
// Validar formulario
 
if (!validateAddAuthorship()) {
 
return;
 
}
 
 
String surname = surnameComboBox.getValue();
 
String name = nameComboBox.getValue();
 
String email = emailTextBox.getValue();
 
 
String acronym = acronymComboBox.getValue();
 
String fullName = fullnameComboBox.getValue();
 
String country = countryTextBox.getValue();
 
Integer isAcademic = isAcademicCheckBox.isChecked() ? 1 : 0;
 
 
// Creamos authorship
 
Authorinstitution authorship = new Authorinstitution();
 
if(selectedAuthor==null) {
 
// Creamos author
 
selectedAuthor = new Author();
 
selectedAuthor.setSurname(surname);
 
selectedAuthor.setName(name);
 
selectedAuthor.setEmail(email);
 
}
 
if(selectedInstitution==null) {
 
// Creamos intitution
 
selectedInstitution = new Institution();
 
selectedInstitution.setAcronym(acronym);
 
selectedInstitution.setFullname(fullName);
 
selectedInstitution.setCountry(country);
 
selectedInstitution.setIsacademic(isAcademic);
 
}
 
authorship.setAuthor(selectedAuthor);
 
authorship.setInstitution(selectedInstitution);
 
 
// Paso de informacion a la pantalla padre
 
Map<String, Object> map = new HashMap<String, Object>();
 
map.put(param, authorship);
 
eq = EventQueues.lookup("interactive", EventQueues.DESKTOP, false);
 
eq.publish(new Event(event, getSelf(), map));
 
// Cierre de la pantalla
 
modalDialog.detach();
 
}
 
 
/**
 
* Permite validar el formulario de nuevo authorship.
 
*
 
* @return true sí todos los datos requeridos son correctos, false en caso
 
* contrario.
 
*/
 
private boolean validateAddAuthorship() {
 
 
String surname = surnameComboBox.getValue();
 
String name = nameComboBox.getValue();
 
String email = emailTextBox.getValue();
 
 
String acronym = acronymComboBox.getValue();
 
String fullName = fullnameComboBox.getValue();
 
 
if (Strings.isBlank(surname) && Strings.isBlank(name)) {
 
String bothBlank = Labels.getLabel("authorship.message");
 
Clients.showNotification(bothBlank, "info", null, "middle_center", 3000);
 
return false;
 
}
 
if (!Strings.isBlank(email) & !email.matches(".+@.+\\.[a-z]+")) {
 
String bothBlank = Labels.getLabel("authorship.message1");
 
Clients.showNotification(bothBlank, "info", emailTextBox, "middle_center", 3000);
 
return false;
 
}
 
if (Strings.isBlank(acronym) && Strings.isBlank(fullName)) {
 
String bothBlank = Labels.getLabel("authorship.message2");
 
Clients.showNotification(bothBlank, "info", null, "middle_center", 3000);
 
return false;
 
}
 
 
return true;
 
}
 
 
/**
 
* Permite inicializar la vista para la inserción de un nuevo authorship.
 
*/
 
@SuppressWarnings({ "unchecked", "rawtypes" })
 
private void initAddAuthorship() {
 
// recogemos todos los autores
 
List<Author> allAuthors = authorService.getAuthors();
 
 
authorsListModel = new ListModelList<>(allAuthors);
 
Comparator myComparator = (Object o1,
 
Object o2) -> ((Author) o2).getSurname().toLowerCase().contains(((String) o1).toLowerCase()) ? 0 : 1;
 
surnameComboBox.setModel(ListModels.toListSubModel(authorsListModel, myComparator, 10));
 
 
authorsListModel1 = new ListModelList<>(allAuthors);
 
Comparator myComparator1 = (Object o1,
 
Object o2) -> ((Author) o2).getName().toLowerCase().contains(((String) o1).toLowerCase()) ? 0 : 1;
 
nameComboBox.setModel(ListModels.toListSubModel(authorsListModel1, myComparator1, 10));
 
 
// recogemos todos las instituciones.
 
List<Institution> allInstitution = institutionService.getInstitutions();
 
 
institutionsListModel = new ListModelList<Institution>(allInstitution);
 
Comparator myComparator2 = (Object o1,
 
Object o2) -> ((Institution) o2).getAcronym().toLowerCase().contains(((String) o1).toLowerCase()) ? 0
 
: 1;
 
acronymComboBox.setModel(ListModels.toListSubModel(institutionsListModel, myComparator2, 10));
 
 
institutionsListModel1 = new ListModelList<Institution>(allInstitution);
 
Comparator myComparator3 = (Object o1,
 
Object o2) -> ((Institution) o2).getAcronym().toLowerCase().contains(((String) o1).toLowerCase()) ? 0
 
: 1;
 
fullnameComboBox.setModel(ListModels.toListSubModel(institutionsListModel1, myComparator3, 10));
 
 
}
 
 
/**
 
* Permite refrescar la vista si un author ha sido seleccionado.
 
*/
 
private void refreshAuthorView() {
 
if(selectedAuthor==null) {
 
surnameComboBox.setValue("");
 
nameComboBox.setValue("");
 
emailTextBox.setValue("");
 
}else {
 
surnameComboBox.setValue(selectedAuthor.getSurname());
 
nameComboBox.setValue(selectedAuthor.getName());
 
emailTextBox.setValue(selectedAuthor.getEmail());
 
}
 
 
}
 
 
/**
 
* Permite refrescar la vista si una institution ha sido seleccionado.
 
*/
 
private void refreshInstitutionView() {
 
if(selectedInstitution==null) {
 
acronymComboBox.setValue("");
 
fullnameComboBox.setValue("");
 
countryTextBox.setValue("");
 
isAcademicCheckBox.setChecked(false);
 
}else {
 
acronymComboBox.setValue(selectedInstitution.getAcronym());
 
fullnameComboBox.setValue(selectedInstitution.getFullname());
 
countryTextBox.setValue(selectedInstitution.getCountry());
 
isAcademicCheckBox.setChecked(selectedInstitution.getIsacademic() > 0 ? true : false);
 
}
 
 
}
 
 
@Listen("onClick = #cancelButton")
 
public void showModal(Event e) {
 
modalDialog.detach();
 
}
 
 
}
Loading