Search in sources :

Example 1 with RichService

use of eu.einfracentral.domain.RichService in project resource-catalogue by madgeek-arc.

the class UserEventsController method favourites.

/**
 * Retrieve all the favourite Services of the authenticated user.
 *
 * @param auth
 * @return
 */
@GetMapping(path = "favourites", produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<List<RichService>> favourites(Authentication auth) {
    Map<String, Float> favouriteServices = new HashMap<>();
    List<Event> userEvents = eventService.getUserEvents(Event.UserActionType.FAVOURITE.getKey(), auth);
    List<RichService> services = new ArrayList<>();
    // Check if the serviceId exists and add it on the list, so to avoid errors
    FacetFilter ff = new FacetFilter();
    ff.setQuantity(10000);
    List<String> serviceIds = new ArrayList<>();
    for (InfraService infraService : infraServiceService.getAll(ff, auth).getResults()) {
        serviceIds.add(infraService.getService().getId());
    }
    for (Event userEvent : userEvents) {
        if (serviceIds.contains(userEvent.getService())) {
            favouriteServices.putIfAbsent(userEvent.getService(), userEvent.getValue());
        }
    }
    for (Map.Entry<String, Float> favouriteService : favouriteServices.entrySet()) {
        if (favouriteService.getValue() == 1) {
            // "1" is true
            services.add(infraServiceService.getRichService(favouriteService.getKey(), "latest", auth));
        }
    }
    return new ResponseEntity<>(services, HttpStatus.OK);
}
Also used : HashMap(java.util.HashMap) FacetFilter(eu.openminted.registry.core.domain.FacetFilter) ArrayList(java.util.ArrayList) InfraService(eu.einfracentral.domain.InfraService) ResponseEntity(org.springframework.http.ResponseEntity) RichService(eu.einfracentral.domain.RichService) Event(eu.einfracentral.domain.Event) HashMap(java.util.HashMap) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with RichService

use of eu.einfracentral.domain.RichService in project resource-catalogue by madgeek-arc.

the class RecommendationManager method getRecommendedResources.

public ResponseEntity<List<RichService>> getRecommendedResources(int limit, Authentication authentication) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(recdbDataSource);
    List<RichService> services = new ArrayList<>();
    /* Get user id */
    int user_id = -1;
    String query = "SELECT user_pk FROM users WHERE user_email = ?;";
    try {
        if (authentication != null) {
            user_id = jdbcTemplate.queryForObject(query, new Object[] { ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail() }, int.class);
        }
        // TODO: get recommendations for non authenticated users
        query = "SELECT service_name " + "FROM services " + "WHERE service_pk IN " + "(SELECT service_id FROM view_count R RECOMMEND R.service_id TO R.user_id ON R.visits USING ItemCosCF WHERE R.user_id = ? ORDER BY R.visits LIMIT ? )";
        List<String> serviceIds = jdbcTemplate.queryForList(query, new Object[] { user_id, limit }, java.lang.String.class);
        String[] ids = serviceIds.toArray(new String[0]);
        services = infraService.getByIds(authentication, ids);
    } catch (DataAccessException e) {
        logger.warn("Could not find user {} in recommendation database.", ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail());
    } catch (Exception e) {
        logger.error(e);
    }
    return ResponseEntity.ok(services);
}
Also used : RichService(eu.einfracentral.domain.RichService) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException) DataAccessException(org.springframework.dao.DataAccessException)

Example 3 with RichService

use of eu.einfracentral.domain.RichService in project resource-catalogue by madgeek-arc.

the class UserEventsController method ratings.

/**
 * Retrieve all the rated Services of the authenticated user.
 *
 * @param auth
 * @return
 */
@GetMapping(path = "ratings", produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<List<RichService>> ratings(Authentication auth) {
    Map<String, Float> serviceRatings = new HashMap<>();
    List<Event> userEvents = eventService.getUserEvents(Event.UserActionType.RATING.getKey(), auth);
    List<RichService> services = new ArrayList<>();
    for (Event userEvent : userEvents) {
        serviceRatings.putIfAbsent(userEvent.getService(), userEvent.getValue());
    }
    for (Map.Entry<String, Float> serviceRating : serviceRatings.entrySet()) {
        services.add(infraServiceService.getRichService(serviceRating.getKey(), "latest", auth));
    }
    return new ResponseEntity<>(services, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) RichService(eu.einfracentral.domain.RichService) ArrayList(java.util.ArrayList) Event(eu.einfracentral.domain.Event) HashMap(java.util.HashMap) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

RichService (eu.einfracentral.domain.RichService)3 ArrayList (java.util.ArrayList)3 Event (eu.einfracentral.domain.Event)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ResponseEntity (org.springframework.http.ResponseEntity)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 InfraService (eu.einfracentral.domain.InfraService)1 FacetFilter (eu.openminted.registry.core.domain.FacetFilter)1 DataAccessException (org.springframework.dao.DataAccessException)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1