widerules / swe574-group1

Automatically exported from code.google.com/p/swe574-group1
0 stars 0 forks source link

getIngredient method is not working properly #47

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This method does not return the foods matching with the criteria. The reason is 
findByCriteria method creates queries with AND keyword. If the parameter is set 
to S%, this method searches the foods with the exact name S%. 

Instead you should write your own JPA query with LIKE keyword. It will be 
something like this:

    public List<Food> findFoods(String name) {
        String sql = "select f from Food f where f.name like :name";

        @SuppressWarnings("unchecked")
        List<Food> resultList = em.createQuery(sql).setParameter("name", name).getResultList();
        return resultList;
    }

And it will be called like this:

List<Food> findFood = DaoFactory.getInstance().getBaseDao().findFood("S%");

Original issue reported on code.google.com by esinkara...@gmail.com on 27 May 2014 at 7:50

GoogleCodeExporter commented 9 years ago
Hi Esin,

Which version are you looking at ? The current version implements a LIKE query, 
but I add the "%" in the end, you shouldn't need to do that at the client side.

Here's the code:

Service
---------
KeyValuePair<String, Object> kvpair = new KeyValuePair<String, Object>("fname", 
queryString + "%");
List<Food> foodList = baseDao.executeNamedQueryGetList("Food.findStartingWith", 
kvpair);

Model
----------
@NamedQueries({
    @NamedQuery(name = "Food.findStartingWith", query = "SELECT f FROM Food f WHERE f.name LIKE :fname")
})

Original comment by turkmen...@gmail.com on 29 May 2014 at 8:15

GoogleCodeExporter commented 9 years ago
In fact - I don't think there ever was a version that I didn't use LIKE.

Original comment by turkmen...@gmail.com on 29 May 2014 at 8:16

GoogleCodeExporter commented 9 years ago
The code (in FoodsService WS) in my workspace is:

// STATUS: untested
    @WebMethod
    public GetIngredientsResponse getIngredients(   @WebParam(name="token")         String token, 
                                                    @WebParam(name="queryString")   String queryString){

        GetIngredientsResponse response = new GetIngredientsResponse();

        if (token == null){
            response.fail(ServiceErrorCode.MISSING_PARAM);
            return response;
        }

        BaseDao baseDao = DaoFactory.getInstance().getBaseDao();

        try {
            ServiceCommons.authenticate(token, response);
        } catch (InvalidTokenException e) {
            response.fail(ServiceErrorCode.TOKEN_INVALID);
            return response;
        } catch (TokenExpiredException e) {
            response.fail(ServiceErrorCode.TOKEN_EXPIRED);
            return response;
        }

        try {
            List<Food> foodList = baseDao.findByCriteria(Food.class, "name", queryString + "%");
            List<FoodInfo> fiList = new ArrayList<FoodInfo>();

            for (Food f : foodList){
                FoodInfo fi = new FoodInfo();
                fi.setFoodId(f.getId());
                fi.setFoodName(f.getName());
                fiList.add(fi);
            }

            response.setListOfIngredients(fiList);
        }
        catch (Exception e){
            response.fail(ServiceErrorCode.INTERNAL_SERVER_ERROR);
            return response;
        }

        response.succeed();
        return response;
    }

Original comment by esinkara...@gmail.com on 29 May 2014 at 11:12

GoogleCodeExporter commented 9 years ago
And also this code seems the latest version in the repository. I've checked the 
repository through Browse feature of GoogleCodes

Original comment by esinkara...@gmail.com on 29 May 2014 at 11:18

GoogleCodeExporter commented 9 years ago
Caner,

You said you used the following query but I couldn't see any named query with 
name the "findStartingWith" in the repository. That's weird.

List<Food> foodList = baseDao.executeNamedQueryGetList("Food.findStartingWith", 
kvpair);

Original comment by esinkara...@gmail.com on 29 May 2014 at 11:27

GoogleCodeExporter commented 9 years ago
OK apparently there was a merge conflict. I remerged, maybe check this version??

Btw, I'm at work and I can only shortly peek at google code now and then

Original comment by turkmen...@gmail.com on 30 May 2014 at 1:37

GoogleCodeExporter commented 9 years ago

Original comment by turkmen...@gmail.com on 1 Jun 2014 at 10:14