Java tutorial
/* * Copyright (C) 2015 Nuuptech * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package com.redhat.rhtracking.persistance.services; import com.redhat.rhtracking.events.EventStatus; import com.redhat.rhtracking.persistance.domain.Catalog; import com.redhat.rhtracking.events.Mappable; import com.redhat.rhtracking.persistance.repository.SingleQuery; import com.redhat.rhtracking.persistance.repository.IterableQuery; import com.redhat.rhtracking.persistance.repository.PageQuery; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.repository.CrudRepository; /** * * @author marco-g8 */ class Queries { private static final Logger logger = Logger.getLogger(Queries.class); public static <T extends Catalog, K extends Serializable> void initCatalog(List<String> catalogue, CrudRepository<T, K> repository, Class<T> type) { logger.info("Starting " + type.getSimpleName() + " catalog"); Iterable<T> typeItr = repository.findAll(); for (T t : typeItr) { if (catalogue.contains(t.toString())) { catalogue.remove(t.toString()); } } if (!catalogue.isEmpty()) { try { for (String s : catalogue) { T missing = type.newInstance(); missing.setType(s); repository.save(missing); } } catch (IllegalAccessException | InstantiationException ex) { logger.error(ex); } } } public static Map<String, Object> addStatus(Map<String, Object> map, EventStatus status) { map.put("status", status); return map; } public static <T extends Mappable> Map<String, Object> findSingle(SingleQuery<T> repo) { T t = repo.query(); if (t != null) return addStatus(t.asMap(), EventStatus.SUCCESS); else return addStatus(new HashMap<String, Object>(), EventStatus.NOT_FOUND); } public static <T extends Mappable> Map<String, Object> findPage(int pageNumber, int pageSize, PageQuery<T> repo) { Page<T> page = repo.query(new PageRequest(pageNumber, pageSize)); List<Map<String, Object>> list = new ArrayList<>(); for (T t : page) { list.add(t.asMap()); } Map<String, Object> result = new HashMap<>(); result.put("page", list); result.put("pageNumber", pageNumber); result.put("pageSize", pageSize); result.put("totalElements", page.getTotalElements()); result.put("totalPages", page.getTotalPages()); result.put("status", EventStatus.SUCCESS); return result; } public static <T extends Mappable> Map<String, Object> findList(IterableQuery<T> repo) { Iterable<T> findAll = repo.query(); List<Map<String, Object>> list = new ArrayList<>(); for (T t : findAll) list.add(t.asMap()); Map<String, Object> response = new HashMap<>(); response.put("list", list); response.put("status", EventStatus.SUCCESS); return response; } static Map<String, Object> findSingle() { throw new UnsupportedOperationException("Not supported yet."); } }