0

I have an Entity which is persisted and which provides data for rows in a Vaadin Grid. This part works.

I now want to display a boolean value as an Image. For this I added a getter to the entity. This part only works when the record has been persisted to the database recently, which I think means that the getter is persisted, which is not desired. For rows persisted to the database before introduction of the getter, the field stays blank in the vaadin grid due to enabledIcon property returning something empty.

I tried to disable this behavior by adding a @Transient annotation, but for some reason then the eureka client has errors, and it also does not help with the empty fields.

I am considering creating a new wrapper class by composition instead of inheritance, but then I have to create all these getters and setters manually which seems to me to be bad design.

Any answer is welcome, even the ones which tell me to go with a wrapper class by composition.

package com.xxx.bpspkpibpcheck.model;

import javax.persistence.Entity;
//import javax.persistence.Transient;

import com.xxx.common.model.KPI;
import com.vaadin.server.ThemeResource;

@Entity
public class KPIBusiness extends KPI {

    private static final String IMAGES = "images/";
    private static final String IMAGE_STATUS_RED = IMAGES + "LED_red_24.png";
    private static final String IMAGE_STATUS_GREEN = IMAGES + "LED_green_24.png";
    private static final String IMAGE_STATUS_GRAY = IMAGES + "LED_gray_24.png";
    private ThemeResource redStatus = new ThemeResource(IMAGE_STATUS_RED);
    private ThemeResource greenStatus = new ThemeResource(IMAGE_STATUS_GREEN);
    private ThemeResource grayStatus = new ThemeResource(IMAGE_STATUS_GRAY);

    //@Transient
    //ThemeResource enabledIcon = greenStatus;

    //@Transient
    public ThemeResource getEnabledIcon()
    {
        return getEnabled() != 0 ? greenStatus : grayStatus;
    }
}
pirho
  • 11,565
  • 12
  • 43
  • 70
Adder
  • 5,708
  • 1
  • 28
  • 56

1 Answers1

2

I might have missed something important here but here is my approach to this one.

To me the problems seems to be that in this case model - your entity - and view / presenter stuff have been mixed in your entity class. And you usually do not want to persist view related stuff.

You should separate this image stuff out of your entity and add the image column as generated column to the grid.

See this as an example: How to add a generated column to Vaadin 8 Grid?

UPDATE: provided example link is not about component column which is needed here so as an example:

Add a component column:

grid.addComponentColumn(statusProvider).setCaption("Status").setId("status");

where statusProvider is like:

ValueProvider<GridEntity, Layout> statusProvider = gridEntity -> {
    AbsoluteLayout al = new AbsoluteLayout();
    al.setSizeUndefined();
    al.addStyleName("status");
    String styleName = (gridEntity.isStatusOk()) ? "green" : "red";
    al.addStyleName(styleName);
    return al;
};

So in my version the "traffic lights" are implemented by cascading css stuff with addStyleName(...) (using the default mytheme.scss) but of course the LEDs images can alos be used either in css or as you originally planned.

.status {
   width: 30px;
   height: 30px;
   border-radius: 15px;
   margin: 0;
   padding: 0;
}

.green {
   background-color: green;
}

.red {
   background-color: red;
}
pirho
  • 11,565
  • 12
  • 43
  • 70