viernes, 23 de abril de 2021

42. Vaadin store additional info in a component using attributes. Grid editor disable components using the stored info in attribute

 0. Introduction

Sometimes we want to disable a component from the grid editor. If the component has information that informs that it should be disabled, then most of the work is done

Most of the code is based on Vaadin grid editor tutorial

Let's see how

  • create a new attribute in the component and store info in this attribute 
  • after invoking "grid.getEditor().editItem(record);" locate the component, read the attribute and if the attribute matches some conditions, then disable the component



1. Let's create the grid and assign a list of records

Grid<Person> grid = new Grid<>();
List<Person> persons = getItems();
grid.setItems(persons);


2. Create a column for the desired field (for instance firstName)

Grid.Column<Person> firstNameColumn =
    grid.addColumn(Person::getFirstName)
       .setHeader("First Name");


3. Create a binder for the Person class

Binder<Person> binder = new Binder<>(Person.class);


4. Extract the grid editor and set this binder

Editor<Person> editor = grid.getEditor();
editor.setBinder(binder);
editor.setBuffered(true);


5. Create a component for instance a TexField for that field

TextField firstNameField = new TextField();


6. Bind the component to the field of the class

binder.bind(firstNameField, "firstName");


7. Optional you can create a new column for storing the buttons Save and Cancel

Grid.Column<Person> editorColumn = grid.addComponentColumn(person ->  {
    Button edit = new Button("Edit");
    edit.addClassName("edit");
    edit.addClickListener(e -> {
        editor.editItem(person);
        firstNameField.focus();
    });
    edit.setEnabled(!editor.isOpen());
    editButtons.add(edit);
    return edit;
});


8. Create a new attribute that indicates that this element cannot be enabled for instance "canenable"

firstNameField.getElement().setAttribute("canenable", "FALSE");


9. when invoking the editor, detect if this attribute is present and disable the component

grid.getEditor().editItem(event.getItem()); 
   //or grid.getEditor().editItem(aPerson)

String canEnable="TRUE";

Element elem =firstNameField.getElement();
if (elem.hasAttribute("canenable"))  
    canEnable=elem.getAttribute("caneable").trim();
if (canEnable.equalsIgnoreCase("FALSE") 
    firstNameField.setEnabled(false);
else firstNameField.setEnabled(true);



10. Now this component is disabled for editing!




jueves, 15 de abril de 2021

41. Common errors

1.persistence.xml: 

a. Verify that all the entities are defined inside a persistence unit 

<class>openadmin.model.gexflow.original.territori.CllCodigoPostal</class>
<class>openadmin.model.gexflow.original.territori.CllComarca</class>
<class>openadmin.model.gexflow.original.territori.CllHueco</class>

b. and also that no other classes are allowed

<exclude-unlisted-classes>true</exclude-unlisted-classes>

c. Verify that the same class (<class>myclass</class>) is not defined in 2 or more persistence units or in case it is duplicated, you must only use one persistence unit in your program


d. Verify which databases are only for query (and not updatable) and use the value "none" to schema modification


<property name="hibernate.hbm2ddl.auto" value="none" /> <!--NO SCHEMA MODIFICATION -->
<property name="hibernate.hbm2ddl.auto" value="update" /> <!--sCHEMA MODIFICATION !!!! -->

2. DB Server corruption

a. Symptoms: After a power failure, when a schema in a Postgres database is accessed from different web servers, one of the servers can fill the schema with "YAMLControlLoad" and cannot see the content of the schema, and the other one can see the content of the schema.

Maybe it is a caché problem or database corruption. If you have a backup, then drop the database and recreate it, and load the backup.

3. Tomcat fails

a. Symptons: Very slow tomcat startup and sometimes fails to deploy heavy applications

1.Save the WAR to deploy from the webapp folder
2. Delete the tomcat directory
3. Download a copy of the apache-tomcat.zip and reinstall it
4. Restore the WAR file into the webapp folder
5. Edit the conf/tomcat-users.xml as seen in Dantesque-Vaadin for allowing the user tomcat and roles tomcat and manager-gui
6. Edit wepapps/manager/META-IONF/context.xml for allowing access to other computers as seen in Dantesque-Vaadin
7. Execute bin/.startup.sh and try if has deloyed the application.