Associate values of a dropdown with a specific column in the BD for query

Asked

Viewed 99 times

0

I have a filter on my site that is in the image below:

inserir a descrição da imagem aqui

The real goal of the same is to "search" travel destinations (Ex:. Paris, Barcelona, Bahia etc...) and present on the screen the same.

I have the following structure of tables represented by bean’s below (with comments in both classes):

Destination.java

@Entity
@Table(name="destination")
public class Destination implements Serializable {
    private static final long serialVersionUID = 1L;

    //Resto dos atributos omitidos para facilitar o etendimento

    //Cada atributo abaixo equivale a uma tabela, na qual possui os atributos no qual será baseado o filtro
    @OneToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name="fk_economic")
    private EconomicProfile economicProfiles;

    @OneToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name="fk_general")
    private GeneralProfile generalProfiles;

    @OneToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name="fk_social")
    private SocialProfile socialProfiles;

    @OneToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name="fk_trip")
    private TripProfile tripProfiles;

    @OneToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name="fk_weather")
    private WeatherProfile weatherprofile;
    //fim dos filtros

    //Getter and Setter
}

Below is an example of a table related to the destination:

@Entity
@Table(name="economic_profile")
public class EconomicProfile implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_economic")
    private Long id;


    //Os atributos abaixo correspondem ao filtro de "Procuro por..." na imagem em anexo.
    @Column(name="economic_travel")
    private Boolean economic;

    @Column(name="intermediate_travel")
    private Boolean intermediate;

    @Column(name="luxury_travel")
    private Boolean luxury;

    //Getter and setter

}

Well, I chose to do it this way, because in each filter when registering the destination the user will be able to choose more than one to register.

Okay, now that the time has come to question me. How to correctly pass the parameters for my action? That is how I can bind by relating the selected value in each dropdown to the corresponding column in my profile?

To be clearer I will give an example below:

<f:form id="form-filter-perfect-travel" modelAttribute="destination" action="${pageContext.request.contextPath}/perfect-travel-filter" method="get" class="form-filtro">

    <div class="box-select">
        <label>PROCURO POR</label>
        <f:select class="select01" path="economicProfiles">
            <option value="">Selecionar...</option>
            <option value="economic">Viagem Económica</option>
            <option value="intermediate">Viagem Intermediária</option>
            <option value="luxury">Viagem de Luxo</option>
        </f:select>
    </div>
</f:form>

GET request:

.../Viatge/Perfect-travel-filter? economicProfiles=Intermediate

I think it has become clearer now. The question is how to take the selected value to pass as a parameter in a query (query that will return my destinies), as the query below example:

select 
    d.id_destination
    d.tenant_id,
    d.appear_website,
    d...
from
    destination d
        inner join
    economic_profile ON id_destination = id_economic
where
    (luxury_travel = ? || economic_travel = ? || intermediate_travel = ?);
  • 1

    With JS you can easily, just include the parameter in the URL in onChange of each selector, for example, no?

  • It would not be the case @Brunocésar. You would need to pass in a certain way the value that was selected in the dropdown and attach it in the correct parameter within the query

  • 1

    Exactly, you’re using spring taglib, the path will generate you the name from select, which is the name you used as parameter. In this example of yours, take name (economicProfiles) and the value of the selected option.

  • I understood Bruno, but the question is this. Each value of my BD column and a Boolean and each value of my select (option) is a text. How can I perform this "crossing"? Is there any other simpler way?

  • 1

    John, you can select only one economicProfile? For example, this is also valid: economicProfiles=intermediate,luxury? I don’t know how your controllers are or how you’re generating queries, but there are many ways to handle this, some you’ll need to handle in the controller/service and pass the object with the options to the persistence layer.

  • Then Bruno I can select only one yes. The problem is how to convert these parameters passed via html to action to be converted to the exact column in the database so yes to perform a select.

Show 1 more comment

1 answer

0


I ended up finding a solution (not very elegant), but ended up serving exactly for my purpose.

In my tag’s of select HTML I passed the name of each attribute equal to the bean on the property of value, just as I show below:

       <select id="social-select"
            class="select01" name="social">
            <option value="">Selecionar...</option>
            <option value="accompanying">Acompanhante</option>
            <option value="alone">Sozinho</option>
            <option value="children">Crianças</option>
            <option value="friends">Amigos</option>
            <option value="elderly">Idosos</option>
            <option value="familyChildren">Sem Crianças</option>
            <option value="teenager">Jovem</option>
        </select>

Therefore, it makes a call via AJAX (this was adopted, because we want to display only a fragment of page, without "updating" it whole). The controller responsible for displaying the result performs a call to my service layer in which makes a Query via JPQL

@Autowired
private CurrentTenantResolver<Long> tenantResolver;

@Autowired
private DestinationRepository destinationRepository;

@PersistenceContext
private EntityManager entityManager;

@SuppressWarnings("unchecked")
public List<Destination> filterDestinations(String economic, String general, String social, String weather, String trip){
    Long currentTenantId = tenantResolver.getCurrentTenantId();     
    String query = "SELECT d FROM Destination d INNER JOIN EconomicProfile e ON d.idDestination = e.id INNER JOIN GeneralProfile g ON d.idDestination = g.id INNER JOIN SocialProfile s ON d.idDestination = s.id INNER JOIN WeatherProfile w ON d.idDestination = w.id INNER JOIN TripProfile t ON d.idDestination = t.id WHERE (e."+economic+" = ?1 and g."+general+" = ?2 and s."+social+" = ?3 and w."+weather+" = ?4 and t."+trip+" = ?5)";        
    entityManager.setProperty(PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT, currentTenantId);
    return entityManager.createQuery(query).setParameter(1, true).setParameter(2, true).setParameter(3, true).setParameter(4, true).setParameter(5, true).getResultList();
}

I use the Spring Data, but currently I adopt the model Saas in my application, soon for technical reasons in which I would have to set my tenant current after having started the transaction I had to choose to perform the query via entityManager. That is to say each option of my tag select corresponds to the column where it should be searched for by the parameter "true" (1).

In that way I was successful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.