@Override public void updateComment(Comment comment) throws NonExistingEntityException, IllegalArgumentException { if (comment == null) { throw new IllegalArgumentException("Comment is null."); } if (comment.getId() == null) { throw new IllegalArgumentException("Comment id is null."); } commentCache = provider.getCacheContainer().getCache("commentcache"); if (!commentCache.containsKey(comment.getId())) { throw new NonExistingEntityException("Comment does not exist in cache."); } try { userTransaction.begin(); commentCache.put(comment.getId(), comment); userTransaction.commit(); logger.info("Comment with id: " + comment.getId() + " was updated in cache store."); } catch (Exception e) { if (userTransaction != null) { try { userTransaction.rollback(); } catch (Exception e1) { e1.printStackTrace(); } } logger.error("Error while updating comment.", e); throw new CacheException(e); } }
public String removeCar(String numberPlate) { carCache = provider.getCacheContainer().getCache(CACHE_NAME); carCache.remove(encode(numberPlate)); List<String> carNumbers = getNumberPlateList(); carNumbers.remove(numberPlate); carCache.replace(CAR_NUMBERS_KEY, carNumbers); return null; }
public String addNewCar() { carCache = provider.getCacheContainer().getCache(CACHE_NAME); carCache.put(CarManager.encode(car.getNumberPlate()), car); List<String> carNumbers = getNumberPlateList(); if (carNumbers == null) carNumbers = new LinkedList<>(); carNumbers.add(car.getNumberPlate()); carCache.replace(CAR_NUMBERS_KEY, carNumbers); return "home"; }
@Override public Comment getCommentById(String id) { if (id == null) { throw new IllegalArgumentException("Comment id is null."); } commentCache = provider.getCacheContainer().getCache("commentcache"); if (commentCache.containsKey(id)) { return (Comment) commentCache.get(id); } else { return null; } }
public static void loadData(BasicCache<String, String> cache, String fileName) throws IOException { try (BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(ScriptingUtils.class.getResourceAsStream(fileName)))) { int chunkSize = 10; int chunkId = 0; CharBuffer cbuf = CharBuffer.allocate(1024 * chunkSize); while (bufferedReader.read(cbuf) >= 0) { Buffer buffer = cbuf.flip(); String textChunk = buffer.toString(); cache.put(fileName + (chunkId++), textChunk); cbuf.clear(); } } }
@Override public void createComment(Comment comment) throws IllegalEntityException, IllegalArgumentException, CacheException { if (comment == null) { throw new IllegalArgumentException("Comment is null."); } if (comment.getId() != null) { throw new IllegalEntityException( "Comment id is not null, Comment entity cannot be put into cache."); } commentCache = provider.getCacheContainer().getCache("commentcache"); comment.setId(UUIDStringGenerator.generateCommentId()); try { userTransaction.begin(); commentCache.put(comment.getId(), comment); userTransaction.commit(); logger.info( "Comment with id: " + comment.getId() + ", songID" + comment.getSongId() + " was inserted to cache store."); } catch (Exception e) { if (userTransaction != null) { try { userTransaction.rollback(); } catch (Exception e1) { e1.printStackTrace(); } } logger.error("Error while putting comment to the comment cache.", e); throw new CacheException(e); } }
public String showCarDetails(String numberPlate) { carCache = provider.getCacheContainer().getCache(CACHE_NAME); this.car = (Car) carCache.get(encode(numberPlate)); return "showdetails"; }
@SuppressWarnings("unchecked") private List<String> getNumberPlateList() { return (List<String>) carCache.get(CAR_NUMBERS_KEY); }