// Path: src/main/java/net/worldline/training/angular/entity/News.java
// @Entity
// public class News {
// 
//     private String category;
//     private String author;
//     private String content;
//     private int likes;
//     @Id
//     @GeneratedValue(strategy= GenerationType.IDENTITY)
//     private int id;
// 
//     public News() {
//     }
// 
// 
//     public String getCategory() {
//         return category;
//     }
// 
//     public void setCategory(String category) {
//         this.category = category;
//     }
// 
//     public String getAuthor() {
//         return author;
//     }
// 
//     public void setAuthor(String author) {
//         this.author = author;
//     }
// 
//     public String getContent() {
//         return content;
//     }
// 
//     public void setContent(String content) {
//         this.content = content;
//     }
// 
//     public int getLikes() {
//         return likes;
//     }
// 
//     public void setLikes(int likes) {
//         this.likes = likes;
//     }
// 
//     public int getId() {
//         return id;
//     }
// 
//     public void setId(int id) {
//         this.id = id;
//     }
// }


import net.worldline.training.angular.entity.News;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import java.util.List;
import java.util.Random;
    	Criteria crit = session.createCriteria(News.class);
    	crit.setProjection(Projections.rowCount());
    	int count = ((Number) crit.uniqueResult()).intValue();
    	if (0 != count) {
    	  int index = new Random().nextInt(count);
    	  crit = session.createCriteria(News.class);
    	  ret =  (News)  crit.setFirstResult(index).setMaxResults(1).uniqueResult();
    	}
    	return ret;
    	
	}
    
    public List<News> getNewsByAuthor(String author) {
        return session.createCriteria(News.class).add(Restrictions.eq("author", author)).addOrder(Order.desc("id")).list();
    }

    
    
    public void add(News news) {
        session.save(news);
    }

    public News incLike(int newsId) {
    	News news = (News) session.createCriteria(News.class).add(Restrictions.eq("id", newsId)).uniqueResult();
    	news.setLikes(news.getLikes()+1);
        session.save(news);
        return news;
    }

    public News delete(int newsId) {
// Path: src/main/java/net/worldline/training/angular/entity/News.java
// @Entity
// public class News {
// 
//     private String category;
//     private String author;
//     private String content;
//     private int likes;
//     @Id
//     @GeneratedValue(strategy= GenerationType.IDENTITY)
//     private int id;
// 
//     public News() {
//     }
// 
// 
//     public String getCategory() {
//         return category;
//     }
// 
//     public void setCategory(String category) {
//         this.category = category;
//     }
// 
//     public String getAuthor() {
//         return author;
//     }
// 
//     public void setAuthor(String author) {
//         this.author = author;
//     }
// 
//     public String getContent() {
//         return content;
//     }
// 
//     public void setContent(String content) {
//         this.content = content;
//     }
// 
//     public int getLikes() {
//         return likes;
//     }
// 
//     public void setLikes(int likes) {
//         this.likes = likes;
//     }
// 
//     public int getId() {
//         return id;
//     }
// 
//     public void setId(int id) {
//         this.id = id;
//     }
// }


// Path: src/main/java/net/worldline/training/angular/services/NewsDAOImpl.java
import net.worldline.training.angular.entity.News;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import java.util.List;
import java.util.Random;

    	Criteria crit = session.createCriteria(News.class);
    	crit.setProjection(Projections.rowCount());
    	int count = ((Number) crit.uniqueResult()).intValue();
    	if (0 != count) {
    	  int index = new Random().nextInt(count);
    	  crit = session.createCriteria(News.class);
    	  ret =  (News)  crit.setFirstResult(index).setMaxResults(1).uniqueResult();
    	}
    	return ret;
    	
	}
    
    public List<News> getNewsByAuthor(String author) {
        return session.createCriteria(News.class).add(Restrictions.eq("author", author)).addOrder(Order.desc("id")).list();
    }

    
    
    public void add(News news) {
        session.save(news);
    }

    public News incLike(int newsId) {
    	News news = (News) session.createCriteria(News.class).add(Restrictions.eq("id", newsId)).uniqueResult();
    	news.setLikes(news.getLikes()+1);
        session.save(news);
        return news;
    }

    public News delete(int newsId) {
    	News news = (News) session.createCriteria(News.class).add(Restrictions.eq("id", newsId)).uniqueResult();