Created
May 28, 2017 12:20
-
-
Save winio94/748df35ac55b119532772c0bf19ec443 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pl.jrj.db.IDbManager; | |
import javax.ejb.EJBException; | |
import javax.naming.InitialContext; | |
import javax.naming.NamingException; | |
import javax.persistence.EntityManager; | |
import javax.persistence.Persistence; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
/** | |
* @author Michał Winnicki | |
* @version 1.0 | |
*/ | |
@Path(value = "/") | |
public class CService { | |
private static String name; | |
private static final SimpleDateFormat FORMAT = | |
new SimpleDateFormat("yyyyMMdd"); | |
private EntityManager entityManager; | |
/** | |
* | |
* @return number of valid insurances today | |
*/ | |
@Path(value = "/valid") | |
@GET | |
@Produces(value = MediaType.TEXT_PLAIN) | |
public Long getTodayValidInsurances() { | |
if (registerSuccessful()) { | |
return getAllValidInsurancesAtDate(today()); | |
} else { | |
return -1L; | |
} | |
} | |
/** | |
* | |
* @param param - path param defining either date or number of days | |
* @return number of valid insurances at given date or | |
* number of valid insurances n days after today | |
*/ | |
@Path(value = "/valid/{param}") | |
@GET | |
@Produces(value = MediaType.TEXT_PLAIN) | |
public Long getInsurancesAfterDateOrNDays(@PathParam("param") | |
String param) { | |
if (registerSuccessful()) { | |
try { | |
Date date = FORMAT.parse(param); | |
return getAllValidInsurancesAtDate(date); | |
} catch (ParseException e) { | |
return getValidInsurancesNDaysAfterDate(param); | |
} | |
} else { | |
return -1L; | |
} | |
} | |
/** | |
* | |
* @param dateParam - path param representing date | |
* @param daysParam - path param representing number of days | |
* @return - number of valid insurances n days after given date | |
*/ | |
@Path(value = "/valid/{dateParam}/{daysParam}") | |
@GET | |
@Produces(value = MediaType.TEXT_PLAIN) | |
public Long getValidInsurancesNDaysAfterDate(@PathParam("dateParam") | |
String dateParam, | |
@PathParam("daysParam") | |
String daysParam) { | |
if (registerSuccessful()) { | |
try { | |
Date date = FORMAT.parse(dateParam); | |
return getValidInsurancesNDaysAfterDate(daysParam, date); | |
} catch (ParseException e) { | |
return getAllValidInsurancesAtDate(today()); | |
} | |
} else { | |
return -1L; | |
} | |
} | |
private Long getValidInsurancesNDaysAfterDate(String param) { | |
try { | |
return getValidInsurancesNDaysAfterDate(param, | |
today()); | |
} catch (NumberFormatException e2) { | |
return getAllValidInsurancesAtDate(today()); | |
} | |
} | |
private boolean registerSuccessful() { | |
try { | |
name = "java:global/ejb-project/DbManager!pl.jrj.db.IDbManager"; | |
IDbManager d = (IDbManager) new InitialContext().lookup(name); | |
return d.register(7, "108225"); | |
} catch (EJBException | NamingException e) { | |
return false; | |
} | |
} | |
private Long getValidInsurancesNDaysAfterDate(String days, Date date) { | |
Integer daysAfterDate = Integer.valueOf(days); | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTime(date); | |
calendar.add(Calendar.DATE, daysAfterDate); | |
return getAllValidInsurancesAtDate(calendar.getTime()); | |
} | |
private Long getAllValidInsurancesAtDate(final Date date) { | |
return getEntityManager().createQuery( | |
"SELECT count(i) from TbInsurance i " + | |
"where i.dateFrom <= :date AND " + | |
"i.dateTo >= :date", Long.class) | |
.setParameter("date", date) | |
.getSingleResult(); | |
} | |
private EntityManager getEntityManager() { | |
if (entityManager == null) { | |
entityManager = Persistence | |
.createEntityManagerFactory("persistence108225") | |
.createEntityManager(); | |
} | |
return entityManager; | |
} | |
private Date today() { | |
return new Date(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment