From 012ad06358af3654bb643bf77ff6dd9645090e0f Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Fri, 22 Jan 2016 14:24:58 +0100 Subject: [PATCH] Implementovano prepinani ciselnych rad podle obdobi. closes #262 --- .../info/bukova/isspst/AppInitListener.java | 54 ++++++---- .../java/info/bukova/isspst/Constants.java | 9 +- .../info/bukova/isspst/dao/SeasonDao.java | 10 ++ .../bukova/isspst/dao/jpa/SeasonDaoJPA.java | 11 ++ .../info/bukova/isspst/data/NumberSeries.java | 14 +++ .../java/info/bukova/isspst/data/Order.java | 41 ++++--- .../bukova/isspst/data/RequirementBase.java | 32 +++--- .../java/info/bukova/isspst/data/Season.java | 79 ++++++++++++++ .../info/bukova/isspst/data/SeasonsAware.java | 8 ++ .../info/bukova/isspst/data/TripBill.java | 15 ++- .../isspst/services/AbstractService.java | 50 ++++++++- .../bukova/isspst/services/HqlDataFilter.java | 87 +++++++++++++++ .../info/bukova/isspst/services/HqlUtils.java | 90 ++++++++++++++++ .../services/dbinfo/DbInfoServiceImpl.java | 75 ++++++++++++- .../numberseries/NumberSeriesService.java | 7 ++ .../numberseries/NumberSeriesServiceImpl.java | 34 +++++- .../services/orders/OrderServiceImpl.java | 11 +- .../requirement/ReqMaterialServiceImpl.java | 44 ++++---- .../requirement/ReqServicesServiceImpl.java | 44 ++++---- .../RequirementBaseServiceImpl.java | 31 ++++-- .../services/settings/SeasonException.java | 18 ++++ .../services/settings/SeasonService.java | 16 +++ .../services/settings/SeasonServiceImpl.java | 100 +++++++++++++++++ .../tripbill/TripBillServiceImpl.java | 8 +- .../info/bukova/isspst/ui/ListViewModel.java | 72 +++++++++++++ .../java/info/bukova/isspst/ui/MainMenu.java | 2 +- .../ui/requirement/TripRequirementList.java | 2 +- .../requirement/TripRequirementListAll.java | 2 +- .../TripRequirementListCentre.java | 2 +- .../TripRequirementListWorkgroup.java | 2 +- .../bukova/isspst/ui/settings/SeasonsVM.java | 101 ++++++++++++++++++ src/main/resources/hibernate.cfg.xml | 1 + .../WEB-INF/locales/zk-label.properties | 13 +++ .../webapp/WEB-INF/spring/root-context.xml | 12 +++ src/main/webapp/app/toolbar.zul | 9 ++ .../main/invoicing/shared/invoicingGrid.zul | 2 +- .../main/orders/requirements/reqListMyAll.zul | 2 + .../orders/requirements/reqListMyCenters.zul | 2 + .../requirements/reqListMyWorkgroups.zul | 2 + src/main/webapp/main/toolbar.zul | 9 ++ .../main/trips/bill/tripBillGridInt.zul | 2 + .../main/trips/requirements/reqListMy.zul | 1 - .../main/trips/requirements/reqListMyAll.zul | 3 +- .../trips/requirements/reqListMyCenters.zul | 3 +- .../requirements/reqListMyWorkgroups.zul | 3 +- src/main/webapp/settings/seasons.zul | 62 +++++++++++ 46 files changed, 1059 insertions(+), 138 deletions(-) create mode 100644 src/main/java/info/bukova/isspst/dao/SeasonDao.java create mode 100644 src/main/java/info/bukova/isspst/dao/jpa/SeasonDaoJPA.java create mode 100644 src/main/java/info/bukova/isspst/data/Season.java create mode 100644 src/main/java/info/bukova/isspst/data/SeasonsAware.java create mode 100644 src/main/java/info/bukova/isspst/services/HqlDataFilter.java create mode 100644 src/main/java/info/bukova/isspst/services/HqlUtils.java create mode 100644 src/main/java/info/bukova/isspst/services/settings/SeasonException.java create mode 100644 src/main/java/info/bukova/isspst/services/settings/SeasonService.java create mode 100644 src/main/java/info/bukova/isspst/services/settings/SeasonServiceImpl.java create mode 100644 src/main/java/info/bukova/isspst/ui/settings/SeasonsVM.java create mode 100644 src/main/webapp/settings/seasons.zul diff --git a/src/main/java/info/bukova/isspst/AppInitListener.java b/src/main/java/info/bukova/isspst/AppInitListener.java index 1314cd04..5a1b2c72 100644 --- a/src/main/java/info/bukova/isspst/AppInitListener.java +++ b/src/main/java/info/bukova/isspst/AppInitListener.java @@ -6,6 +6,7 @@ import info.bukova.isspst.data.NumberSeries; import info.bukova.isspst.data.Permission; import info.bukova.isspst.data.RequirementType; import info.bukova.isspst.data.Role; +import info.bukova.isspst.data.Season; import info.bukova.isspst.data.SettingsData; import info.bukova.isspst.data.User; import info.bukova.isspst.reporting.Report; @@ -17,6 +18,7 @@ import info.bukova.isspst.services.munits.MUnitService; import info.bukova.isspst.services.numberseries.NumberSeriesService; import info.bukova.isspst.services.requirement.RequirementTypeService; import info.bukova.isspst.services.settings.GlobalSettingsService; +import info.bukova.isspst.services.settings.SeasonService; import info.bukova.isspst.services.users.PermissionService; import info.bukova.isspst.services.users.RoleService; import info.bukova.isspst.services.users.UserService; @@ -29,6 +31,7 @@ import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import java.math.BigDecimal; +import java.util.Calendar; import java.util.List; public class AppInitListener implements ServletContextListener { @@ -43,7 +46,8 @@ public class AppInitListener implements ServletContextListener { private RequirementTypeService reqTypeService; private GlobalSettingsService gSettingsService; private FullTextService ftService; - + private SeasonService seasonService; + @Override public void contextDestroyed(ServletContextEvent arg0) { @@ -64,6 +68,7 @@ public class AppInitListener implements ServletContextListener { gSettingsService = ctx.getBean(GlobalSettingsService.class); reqTypeService = ctx.getBean(RequirementTypeService.class); ftService = ctx.getBean(FullTextService.class); + seasonService = ctx.getBean(SeasonService.class); userService.grantAdmin(); this.checkDbInfo(); @@ -285,29 +290,36 @@ public class AppInitListener implements ServletContextListener { } } } - + + private void checkNumberSeriesForSeason(Season season) { + for (String modId : Constants.NUM_SERIES_PREFIX.keySet()) { + NumberSeries ns = nsService.getNumberSerieForSeason(modId, season); + if (ns == null) { + ns = new NumberSeries(); + ns.setModule(modId); + ns.setPrefix(Constants.NUM_SERIES_PREFIX.get(modId)); + ns.setNumber(1); + ns.setSeason(season); + nsService.add(ns); + } + } + } + private void checkNumberSeries() { - NumberSeries ns = nsService.getNumberSerie(Constants.MOD_REQUIREMENTS); - - if (ns == null) - { - ns = new NumberSeries(); - ns.setModule(Constants.MOD_REQUIREMENTS); - ns.setPrefix(""); - ns.setNumber(1); - nsService.add(ns); + Season season = seasonService.getActive(); + + if (season == null) { + season = new Season(); + Calendar cal = Calendar.getInstance(); + season.setDescription(String.valueOf(cal.get(Calendar.YEAR))); + season.setValidFrom(cal.getTime()); + season.setActive(true); + seasonService.add(season); } - - ns = nsService.getNumberSerie(Constants.MOD_ORDER); - - if (ns == null) - { - ns = new NumberSeries(); - ns.setModule(Constants.MOD_ORDER); - ns.setPrefix(""); - ns.setNumber(1); - nsService.add(ns); + + for (Season s : seasonService.getAllSeasons()) { + checkNumberSeriesForSeason(s); } } diff --git a/src/main/java/info/bukova/isspst/Constants.java b/src/main/java/info/bukova/isspst/Constants.java index f8c60ee8..19f6b1e8 100644 --- a/src/main/java/info/bukova/isspst/Constants.java +++ b/src/main/java/info/bukova/isspst/Constants.java @@ -35,7 +35,7 @@ import java.util.Map; public class Constants { - public final static long DB_VERSION = 5; + public final static long DB_VERSION = 6; public final static String DEF_ADMIN = "admin"; public final static String DEF_ADMIN_PASSWD = "admin"; @@ -151,10 +151,17 @@ public class Constants { new ReportMapping(MOD_REQUIREMENTS, new Report(8, true, "Protokol o kontrole", "orderRequirement", false, true)) }; + // pokud je v agnde vic nez jedena podepisovaci sestava, musi se definovat ktera sestava nalezi jake entite public final static Map, Integer> SIGN_REPORT_MAP = Collections.unmodifiableMap(new HashMap, Integer>() {{ put(TripBillApproval.class, 4); put(TripRequirement.class, 7); }}); + + // vychozi prefixy ciselnych rad. Pokud se prida agenda s vlastni ciselnou radou, je potreba zadefinovat vychozi prefix + public final static Map NUM_SERIES_PREFIX = Collections.unmodifiableMap(new HashMap() {{ + put(MOD_REQUIREMENTS, "P"); + put(MOD_ORDER, "O"); + }}); public final static String REQTYPE_ORDER = "ORDER"; public final static String REQTYPE_BUSINESSTRIP = "BUSINESSTRIP"; diff --git a/src/main/java/info/bukova/isspst/dao/SeasonDao.java b/src/main/java/info/bukova/isspst/dao/SeasonDao.java new file mode 100644 index 00000000..a54965dd --- /dev/null +++ b/src/main/java/info/bukova/isspst/dao/SeasonDao.java @@ -0,0 +1,10 @@ +package info.bukova.isspst.dao; + +import info.bukova.isspst.data.Season; + +/** + * @author Pepa Rokos + */ +public interface SeasonDao extends BaseDao { + +} diff --git a/src/main/java/info/bukova/isspst/dao/jpa/SeasonDaoJPA.java b/src/main/java/info/bukova/isspst/dao/jpa/SeasonDaoJPA.java new file mode 100644 index 00000000..5d1ec12d --- /dev/null +++ b/src/main/java/info/bukova/isspst/dao/jpa/SeasonDaoJPA.java @@ -0,0 +1,11 @@ +package info.bukova.isspst.dao.jpa; + +import info.bukova.isspst.dao.SeasonDao; +import info.bukova.isspst.data.Season; + +/** + * @author Pepa Rokos + */ +public class SeasonDaoJPA extends BaseDaoJPA implements SeasonDao { + +} diff --git a/src/main/java/info/bukova/isspst/data/NumberSeries.java b/src/main/java/info/bukova/isspst/data/NumberSeries.java index a26e2538..a0981a6d 100644 --- a/src/main/java/info/bukova/isspst/data/NumberSeries.java +++ b/src/main/java/info/bukova/isspst/data/NumberSeries.java @@ -2,6 +2,8 @@ package info.bukova.isspst.data; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @@ -17,6 +19,10 @@ public class NumberSeries extends BaseSimpleData @Column(name = "NUMBER") private int number; + @OneToOne + @JoinColumn(name = "SEASON_ID") + private Season season; + public String getCurrentNumber() { return String.format("%s%06d", this.getPrefix(), this.getNumber()); @@ -51,4 +57,12 @@ public class NumberSeries extends BaseSimpleData { this.number = number; } + + public Season getSeason() { + return season; + } + + public void setSeason(Season season) { + this.season = season; + } } diff --git a/src/main/java/info/bukova/isspst/data/Order.java b/src/main/java/info/bukova/isspst/data/Order.java index 22cbe2ec..6b677b74 100644 --- a/src/main/java/info/bukova/isspst/data/Order.java +++ b/src/main/java/info/bukova/isspst/data/Order.java @@ -1,11 +1,13 @@ package info.bukova.isspst.data; import info.bukova.isspst.Constants; - -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import org.hibernate.annotations.LazyCollection; +import org.hibernate.annotations.LazyCollectionOption; +import org.hibernate.search.annotations.Analyze; +import org.hibernate.search.annotations.Field; +import org.hibernate.search.annotations.Index; +import org.hibernate.search.annotations.Indexed; +import org.hibernate.search.annotations.IndexedEmbedded; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; @@ -13,21 +15,20 @@ import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; - -import org.hibernate.annotations.LazyCollection; -import org.hibernate.annotations.LazyCollectionOption; -import org.hibernate.search.annotations.Analyze; -import org.hibernate.search.annotations.Field; -import org.hibernate.search.annotations.Index; -import org.hibernate.search.annotations.Indexed; -import org.hibernate.search.annotations.IndexedEmbedded; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; @Entity @Table(name = "ORDERS") @Indexed -public class Order extends BaseData implements Cloneable +public class Order extends BaseData implements Cloneable, SeasonsAware { @Column(name = "NUMSER") @@ -121,6 +122,10 @@ public class Order extends BaseData implements Cloneable @Column(name = "DELIVERED") private boolean delivered; + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "SEASON_ID") + private Season season; + public Order() { this.items = new ArrayList(); @@ -288,4 +293,12 @@ public class Order extends BaseData implements Cloneable cloned.setTotal(new BigDecimal(this.getTotal().toString())); return cloned; } + + public Season getSeason() { + return season; + } + + public void setSeason(Season season) { + this.season = season; + } } diff --git a/src/main/java/info/bukova/isspst/data/RequirementBase.java b/src/main/java/info/bukova/isspst/data/RequirementBase.java index b6fc5241..a7e60f59 100644 --- a/src/main/java/info/bukova/isspst/data/RequirementBase.java +++ b/src/main/java/info/bukova/isspst/data/RequirementBase.java @@ -1,10 +1,12 @@ package info.bukova.isspst.data; import info.bukova.isspst.Constants; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import org.hibernate.annotations.LazyCollection; +import org.hibernate.annotations.LazyCollectionOption; +import org.hibernate.search.annotations.Analyze; +import org.hibernate.search.annotations.Field; +import org.hibernate.search.annotations.Index; +import org.hibernate.search.annotations.Indexed; import javax.persistence.CascadeType; import javax.persistence.Column; @@ -16,17 +18,13 @@ import javax.persistence.ManyToOne; import javax.persistence.MappedSuperclass; import javax.persistence.OneToMany; import javax.persistence.OrderBy; - -import org.hibernate.annotations.LazyCollection; -import org.hibernate.annotations.LazyCollectionOption; -import org.hibernate.search.annotations.Analyze; -import org.hibernate.search.annotations.Field; -import org.hibernate.search.annotations.Index; -import org.hibernate.search.annotations.Indexed; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; @MappedSuperclass @Indexed -public class RequirementBase extends BaseData implements FilterableRequirement { +public class RequirementBase extends BaseData implements FilterableRequirement, SeasonsAware { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "TYPE_ID") @@ -56,6 +54,9 @@ public class RequirementBase extends BaseData implements FilterableRequirement { @Column(name = "STATE") @Enumerated(EnumType.ORDINAL) private RequirementState state; + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "SEASON_ID") + private Season season; public RequirementBase() { authorization = new ArrayList(); @@ -152,4 +153,11 @@ public class RequirementBase extends BaseData implements FilterableRequirement { return authorization.get(authorization.size() - 1).getAuthDate(); } + public Season getSeason() { + return season; + } + + public void setSeason(Season season) { + this.season = season; + } } diff --git a/src/main/java/info/bukova/isspst/data/Season.java b/src/main/java/info/bukova/isspst/data/Season.java new file mode 100644 index 00000000..b7e99e9e --- /dev/null +++ b/src/main/java/info/bukova/isspst/data/Season.java @@ -0,0 +1,79 @@ +package info.bukova.isspst.data; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; +import java.util.Date; + +/** + * @author Pepa Rokos + */ +@Entity +@Table(name = "SEASON") +public class Season extends BaseData { + @Column(name = "DESCRIPTION") + private String description; + @Column(name = "VALID_FROM") + private Date validFrom; + @Column(name = "VALID_TO") + private Date validTo; + @Column(name = "ACTIVE") + private boolean active; + + public String getDescription() { + return description; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Season)) return false; + + Season season = (Season) o; + + if (getId() != season.getId()) return false; + if (active != season.active) return false; + if (description != null ? !description.equals(season.description) : season.description != null) return false; + if (validFrom != null ? !validFrom.equals(season.validFrom) : season.validFrom != null) return false; + if (validTo != null ? !validTo.equals(season.validTo) : season.validTo != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = description != null ? description.hashCode() : 0; + result = 31 * result + (validFrom != null ? validFrom.hashCode() : 0); + result = 31 * result + (validTo != null ? validTo.hashCode() : 0); + result = 31 * result + (active ? 1 : 0); + return result; + } + + public void setDescription(String description) { + this.description = description; + } + + public Date getValidFrom() { + return validFrom; + } + + public void setValidFrom(Date validFrom) { + this.validFrom = validFrom; + } + + public Date getValidTo() { + return validTo; + } + + public void setValidTo(Date validTo) { + this.validTo = validTo; + } + + public boolean isActive() { + return active; + } + + public void setActive(boolean active) { + this.active = active; + } +} diff --git a/src/main/java/info/bukova/isspst/data/SeasonsAware.java b/src/main/java/info/bukova/isspst/data/SeasonsAware.java new file mode 100644 index 00000000..ba8a24d6 --- /dev/null +++ b/src/main/java/info/bukova/isspst/data/SeasonsAware.java @@ -0,0 +1,8 @@ +package info.bukova.isspst.data; + +/** + * @author Pepa Rokos + */ +public interface SeasonsAware { + public Season getSeason(); +} diff --git a/src/main/java/info/bukova/isspst/data/TripBill.java b/src/main/java/info/bukova/isspst/data/TripBill.java index 494c383c..6db8d6d3 100644 --- a/src/main/java/info/bukova/isspst/data/TripBill.java +++ b/src/main/java/info/bukova/isspst/data/TripBill.java @@ -15,6 +15,7 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.Table; @@ -26,7 +27,7 @@ import java.util.List; @Entity @Table(name = "TRIP_BILL") @Indexed -public class TripBill extends BaseData implements EntityWithAttachment { +public class TripBill extends BaseData implements EntityWithAttachment, SeasonsAware { @OneToOne(fetch = FetchType.EAGER) @JoinColumn(name = "REQUIREMENT_ID") @@ -69,6 +70,9 @@ public class TripBill extends BaseData implements EntityWithAttachment { private Boolean paid; @Column(name = "PAID_DATE") private Date paidDate; + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "SEASON_ID") + private Season season; public TripBill() { billItems = new ArrayList(); @@ -206,4 +210,13 @@ public class TripBill extends BaseData implements EntityWithAttachment { public void setPaidDate(Date paidDate) { this.paidDate = paidDate; } + + @Override + public Season getSeason() { + return season; + } + + public void setSeason(Season season) { + this.season = season; + } } diff --git a/src/main/java/info/bukova/isspst/services/AbstractService.java b/src/main/java/info/bukova/isspst/services/AbstractService.java index b199d869..6abb4a19 100644 --- a/src/main/java/info/bukova/isspst/services/AbstractService.java +++ b/src/main/java/info/bukova/isspst/services/AbstractService.java @@ -38,6 +38,8 @@ public abstract class AbstractService implements Service @Autowired protected SessionData sessionData; @Autowired + protected HqlDataFilter hqlFilter; + @Autowired protected QueryDao queryDao; private DocumentFileStorage documentFileStorage; @@ -88,6 +90,50 @@ public abstract class AbstractService implements Service } } + protected Query filterQuery(Query q) { + return filterQuery(q, null); + } + + protected Query filterQuery(Query q, HqlDataFilter aditFilter) { + HqlDataFilter f = hqlFilter; + try { + if (((f.getWhere() == null || f.getWhere().isEmpty()) + && (f.getOrderBy() == null || f.getOrderBy().isEmpty())) + && ((aditFilter.getWhere() == null || aditFilter.getWhere().isEmpty()) + && (aditFilter.getOrderBy() == null || aditFilter.getOrderBy().isEmpty()))) { + return q; + } + } catch (Exception e) { + f = new HqlDataFilter(); // pri volani mimo web request filtr z requestu neresime + } + + boolean isAplicable = false; + if (f.getDataClass() != null) { + for (Class clazz : getEntityClasses()) { + if (f.getDataClass().isAssignableFrom(clazz)) { + isAplicable = true; + } + } + } + + String hql; + if (isAplicable) { + hql = HqlUtils.buildHql(q.getQueryString(), f); + } else { + hql = q.getQueryString(); + } + + hql = HqlUtils.addOrderBy(HqlUtils.addAndWhere(hql, aditFilter), aditFilter); + Query newQuery = dao.getQuery(hql); + + if (isAplicable) { + HqlUtils.addParameters(newQuery, f); + } + + HqlUtils.addParameters(newQuery, aditFilter); + return newQuery; + } + @Override @PreAuthorize("hasPermission(this, 'PERM_ADD')") public final T create() { @@ -199,8 +245,10 @@ public abstract class AbstractService implements Service if (getModule() != null && !getModule().isActive()) { throw new ModuleNotActiveException(); } + + Query q = dao.getQuery("from " + dao.getEntityName()); - return dao.getAll(); + return filterQuery(q).list(); } @Override diff --git a/src/main/java/info/bukova/isspst/services/HqlDataFilter.java b/src/main/java/info/bukova/isspst/services/HqlDataFilter.java new file mode 100644 index 00000000..74c4a0b9 --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/HqlDataFilter.java @@ -0,0 +1,87 @@ +package info.bukova.isspst.services; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * @author Pepa Rokos + */ +public class HqlDataFilter { + private String where; + private String orderBy; + private Map params; + private Class dataClass; + + public HqlDataFilter() { + params = new HashMap(); + } + + public HqlDataFilter(String where) { + this(); + this.where = where; + } + + public HqlDataFilter(String where, String orderBy) { + this(where); + this.orderBy = orderBy; + } + + public String getWhere() { + return where; + } + + public void setWhere(String where) { + this.where = where; + } + + public String getOrderBy() { + return orderBy; + } + + public void setOrderBy(String orderBy) { + this.orderBy = orderBy; + } + + public void setParam(String key, Object value) { + params.put(key, value); + } + + public Object getParam(String key) { + return params.get(key); + } + + public Set getParamKeys() { + return params.keySet(); + } + + public void andFilter(String filter) { + if (where == null || where.isEmpty()) { + where = filter; + } else { + where += " and (" + filter + ")"; + } + } + + public void orFilter(String filter) { + if (where == null ||where.isEmpty()) { + where = filter; + } else { + where += " or (" + filter + ")"; + } + } + + public void reset() { + where = ""; + orderBy = ""; + params.clear(); + } + + public Class getDataClass() { + return dataClass; + } + + public void setDataClass(Class dataClass) { + this.dataClass = dataClass; + } +} diff --git a/src/main/java/info/bukova/isspst/services/HqlUtils.java b/src/main/java/info/bukova/isspst/services/HqlUtils.java new file mode 100644 index 00000000..491d3583 --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/HqlUtils.java @@ -0,0 +1,90 @@ +package info.bukova.isspst.services; + +import org.hibernate.Query; + +import java.util.Collection; + +/** + * @author Pepa Rokos + */ +public class HqlUtils { + + public static String buildHql(String select, HqlDataFilter filter) { + if (select.toLowerCase().contains("where") + || select.toLowerCase().contains("order by")) { + throw new IsspstException("Use add*() methods instead."); + } + + String hql = addWhere(select, filter, ""); + return addOrderBy(hql, filter); + } + + public static String addAndWhere(String hql, HqlDataFilter filter) { + if (!checkWhere(filter)) { + return hql; + } + + return addWhere(hql, filter, "and"); + } + + public static String addOrWhere(String hql, HqlDataFilter filter) { + if (!checkWhere(filter)) { + return hql; + } + + return addWhere(hql, filter, "or"); + } + + public static String addOrderBy(String hql, HqlDataFilter filter) { + if (filter == null || filter.getOrderBy() == null || filter.getOrderBy().isEmpty()) { + return hql; + } + + if (!hql.contains("order by")) { + hql += " order by "; + } + + hql += filter.getOrderBy(); + return hql; + } + + public static void addParameters(Query query, HqlDataFilter filter) { + if (filter == null) { + return; + } + + for (String key : filter.getParamKeys()) { + if (filter.getParam(key) instanceof Collection) { + query.setParameterList(key, (Collection) filter.getParam(key)); + } else { + query.setParameter(key, filter.getParam(key)); + } + } + } + + private static boolean checkWhere(HqlDataFilter filter) { + return filter != null && filter.getWhere() != null && !filter.getWhere().isEmpty(); + } + + private static String addWhere(String hql, HqlDataFilter filter, String operator) { + if (hql.toLowerCase().contains("where") && hql.toLowerCase().contains("order by")) { + String[] split = hql.split("order by"); + hql = split[0] + " " + operator + " (" + filter.getWhere() + ") order by " + split[1]; + return hql; + } + + if (hql.toLowerCase().contains("where")) { + hql += " " + operator + " (" + filter.getWhere() + ")"; + return hql; + } + + if (hql.toLowerCase().contains("order by")) { + String[] split = hql.split("order by"); + hql = split[0] + " where (" + filter.getWhere() + ") order by " + split[1]; + return hql; + } + + hql += " where (" + filter.getWhere() + ")"; + return hql; + } +} diff --git a/src/main/java/info/bukova/isspst/services/dbinfo/DbInfoServiceImpl.java b/src/main/java/info/bukova/isspst/services/dbinfo/DbInfoServiceImpl.java index 60c60a6e..c757edac 100644 --- a/src/main/java/info/bukova/isspst/services/dbinfo/DbInfoServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/dbinfo/DbInfoServiceImpl.java @@ -3,13 +3,12 @@ package info.bukova.isspst.services.dbinfo; import info.bukova.isspst.Constants; import info.bukova.isspst.data.DbInfo; import info.bukova.isspst.services.AbstractService; +import org.hibernate.SQLQuery; +import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; -import org.hibernate.SQLQuery; -import org.springframework.transaction.annotation.Transactional; - public class DbInfoServiceImpl extends AbstractService implements DbInfoService { private DbInfo getDbInfo() @@ -142,6 +141,76 @@ public class DbInfoServiceImpl extends AbstractService implements DbInfo sq.executeUpdate(); } + if (dbVersion > 0 && dbVersion < 6) { + sql = "INSERT INTO SEASON(DESCRIPTION, VALID_FROM, VALID_TO, ACTIVE) " + + "VALUES ('2015', '2015-01-01', '2015-12-31', 0)"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + + sql = "INSERT INTO SEASON(DESCRIPTION, VALID_FROM, VALID_TO, ACTIVE) " + + "VALUES ('2016', '2016-01-01', NULL, 1)"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + + sql = "UPDATE NUMBERSERIES SET SEASON_ID = 2"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + + sql = "INSERT INTO NUMBERSERIES(MODULE, PREFIX, NUMBER, SEASON_ID) " + + "VALUES ('REQUIREMENTS', 'P', 2528, 1)"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + + sql = "INSERT INTO NUMBERSERIES(MODULE, PREFIX, NUMBER, SEASON_ID) " + + "VALUES ('ORDER', 'OBJ', 55, 1)"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + + // rozdeleni dat do sezon + sql = "UPDATE REQUIREMENT SET SEASON_ID = 2 WHERE NUMSER LIKE 'P16%'"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + sql = "UPDATE REQUIREMENT SET SEASON_ID = 1 WHERE NUMSER LIKE 'P0%'"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + + sql = "UPDATE TRIPREQUIREMENT SET SEASON_ID = 2 WHERE NUMSER LIKE 'P16%'"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + sql = "UPDATE TRIPREQUIREMENT SET SEASON_ID = 1 WHERE NUMSER LIKE 'P0%'"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + + sql = "UPDATE ORDERS SET SEASON_ID = 2 WHERE NUMSER LIKE 'OBJ16%'"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + sql = "UPDATE ORDERS SET SEASON_ID = 1 WHERE NUMSER LIKE 'OBJ0%'"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + + sql = "UPDATE TRIP_BILL_APPROVAL SET SEASON_ID = 2 " + + " WHERE TRIPBILL_ID in " + + " (SELECT tb.ID FROM TRIP_BILL AS tb " + + " JOIN TRIPREQUIREMENT ON TRIPREQUIREMENT.ID = tb.REQUIREMENT_ID WHERE NUMSER LIKE 'P16%')"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + sql = "UPDATE TRIP_BILL_APPROVAL SET SEASON_ID = 1 " + + "WHERE TRIPBILL_ID in " + + "(SELECT tb.ID FROM TRIP_BILL AS tb " + + "JOIN TRIPREQUIREMENT ON TRIPREQUIREMENT.ID = tb.REQUIREMENT_ID WHERE NUMSER LIKE 'P0%')"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + + sql = "UPDATE TRIP_BILL tb JOIN TRIPREQUIREMENT ON TRIPREQUIREMENT.ID = tb.REQUIREMENT_ID " + + "SET tb.SEASON_ID = 2 WHERE NUMSER LIKE 'P16%'"; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + sql = "UPDATE TRIP_BILL tb JOIN TRIPREQUIREMENT ON TRIPREQUIREMENT.ID = tb.REQUIREMENT_ID " + + "SET tb.SEASON_ID = 1 WHERE NUMSER LIKE 'P0%'";; + sq = dao.getSession().createSQLQuery(sql); + sq.executeUpdate(); + } + this.updateDatabaseVersion(); } } diff --git a/src/main/java/info/bukova/isspst/services/numberseries/NumberSeriesService.java b/src/main/java/info/bukova/isspst/services/numberseries/NumberSeriesService.java index 37671f10..9a6dea7f 100644 --- a/src/main/java/info/bukova/isspst/services/numberseries/NumberSeriesService.java +++ b/src/main/java/info/bukova/isspst/services/numberseries/NumberSeriesService.java @@ -1,11 +1,18 @@ package info.bukova.isspst.services.numberseries; import info.bukova.isspst.data.NumberSeries; +import info.bukova.isspst.data.Season; import info.bukova.isspst.services.Service; +import java.util.List; + public interface NumberSeriesService extends Service { public NumberSeries getNumberSerie(String module); + + public NumberSeries getNumberSerieForSeason(String module, Season season); public void increase(NumberSeries ns); + + public List getSeriesForSeason(Season season); } diff --git a/src/main/java/info/bukova/isspst/services/numberseries/NumberSeriesServiceImpl.java b/src/main/java/info/bukova/isspst/services/numberseries/NumberSeriesServiceImpl.java index fc275425..adcce176 100644 --- a/src/main/java/info/bukova/isspst/services/numberseries/NumberSeriesServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/numberseries/NumberSeriesServiceImpl.java @@ -1,24 +1,48 @@ package info.bukova.isspst.services.numberseries; import info.bukova.isspst.data.NumberSeries; +import info.bukova.isspst.data.Season; import info.bukova.isspst.services.AbstractService; +import info.bukova.isspst.services.settings.SeasonService; +import org.hibernate.Query; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + public class NumberSeriesServiceImpl extends AbstractService implements NumberSeriesService { + @Autowired + private SeasonService seasonService; + + @Transactional + @Override + public NumberSeries getNumberSerie(String module) { + return getNumberSerieForSeason(module, seasonService.getActive()); + } + @Transactional @Override - public NumberSeries getNumberSerie(String module) - { - return this.selectSingle("from NumberSeries where MODULE = '" + module + "'"); + public NumberSeries getNumberSerieForSeason(String module, Season season) { + Query q = dao.getQuery("from NumberSeries where module = :m and season = :s"); + q.setParameter("m", module); + q.setParameter("s", season); + return (NumberSeries) q.uniqueResult(); } @Transactional @Override - public void increase(NumberSeries ns) - { + public void increase(NumberSeries ns) { ns.setNumber(ns.getNumber() + 1); this.update(ns); } + + @Transactional + @Override + public List getSeriesForSeason(Season season) { + Query q = dao.getQuery("from NumberSeries where season = :s"); + q.setParameter("s", season); + return q.list(); + } } diff --git a/src/main/java/info/bukova/isspst/services/orders/OrderServiceImpl.java b/src/main/java/info/bukova/isspst/services/orders/OrderServiceImpl.java index 801cf13c..27ecdc55 100644 --- a/src/main/java/info/bukova/isspst/services/orders/OrderServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/orders/OrderServiceImpl.java @@ -10,16 +10,15 @@ import info.bukova.isspst.data.Workgroup; import info.bukova.isspst.services.AbstractOwnedService; import info.bukova.isspst.services.LazyLoader; import info.bukova.isspst.services.settings.GlobalSettingsService; - -import java.math.BigDecimal; -import java.util.Date; -import java.util.List; - import org.hibernate.Hibernate; import org.hibernate.Query; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; + +import java.math.BigDecimal; +import java.util.Date; +import java.util.List; public class OrderServiceImpl extends AbstractOwnedService implements OrderService { @@ -76,7 +75,7 @@ public class OrderServiceImpl extends AbstractOwnedService implements public List getAll() { Query q = dao.getQuery("select o from Order o join fetch o.ownedBy"); - return q.list(); + return filterQuery(q).list(); } @LazyLoader("form") diff --git a/src/main/java/info/bukova/isspst/services/requirement/ReqMaterialServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/ReqMaterialServiceImpl.java index e3096b84..096a819b 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/ReqMaterialServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/ReqMaterialServiceImpl.java @@ -3,17 +3,14 @@ package info.bukova.isspst.services.requirement; import info.bukova.isspst.Constants; import info.bukova.isspst.data.Requirement; import info.bukova.isspst.data.RequirementState; -import info.bukova.isspst.data.Workgroup; - -import java.util.Date; -import java.util.List; - -import org.hibernate.Query; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PostFilter; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; +import java.util.Date; +import java.util.List; + public class ReqMaterialServiceImpl extends RequirementServiceImpl implements RequirementService, RequirementBaseService { @Autowired @@ -38,10 +35,10 @@ public class ReqMaterialServiceImpl extends RequirementServiceImpl implements Re @PreAuthorize("hasPermission(this, 'PERM_READ')") public List getMy() { - Query q = dao.getQuery("from " + dao.getEntityName() + " where ownedBy = :owner and kind = :kind"); - q.setParameter("owner", getLoggedInUser()); - q.setParameter("kind", Constants.REQ_TYPE_MATERIAL); - return q.list(); + hqlFilter.setDataClass(Requirement.class); + hqlFilter.andFilter("kind = :kind"); + hqlFilter.setParam("kind", Constants.REQ_TYPE_MATERIAL); + return super.getMy(); } @SuppressWarnings("unchecked") @@ -51,11 +48,10 @@ public class ReqMaterialServiceImpl extends RequirementServiceImpl implements Re @PostFilter("hasPermission(filterObject, 'PERM_SHOW_CENTRE_REQ')") public List getCentreReq() { - List wgList = workgroupService.getUserCentres(getLoggedInUser()); - Query q = dao.getQuery("select tr from " + dao.getEntityName() + " tr join fetch tr.ownedBy join tr.centre c where c in (:wgList) and kind = :kind order by tr.numser"); - q.setParameterList("wgList", wgList); - q.setParameter("kind", Constants.REQ_TYPE_MATERIAL); - return q.list(); + hqlFilter.setDataClass(Requirement.class); + hqlFilter.andFilter("kind = :kind"); + hqlFilter.setParam("kind", Constants.REQ_TYPE_MATERIAL); + return super.getCentreReq(); } @SuppressWarnings("unchecked") @@ -65,13 +61,10 @@ public class ReqMaterialServiceImpl extends RequirementServiceImpl implements Re @PostFilter("hasPermission(filterObject, 'PERM_SHOW_WORKGROUP_REQ')") public List getWorkgroupReq() { - List wgList = workgroupService.getUserWorkgroups(getLoggedInUser()); - Query q = dao.getQuery("select tr from " - + dao.getEntityName() - + " tr join fetch tr.ownedBy join tr.workgroup w where w in (:wgList) and kind = :kind order by tr.numser"); - q.setParameterList("wgList", wgList); - q.setParameter("kind", Constants.REQ_TYPE_MATERIAL); - return q.list(); + hqlFilter.setDataClass(Requirement.class); + hqlFilter.andFilter("kind = :kind"); + hqlFilter.setParam("kind", Constants.REQ_TYPE_MATERIAL); + return super.getWorkgroupReq(); } @SuppressWarnings("unchecked") @@ -80,8 +73,9 @@ public class ReqMaterialServiceImpl extends RequirementServiceImpl implements Re @PreAuthorize("hasPermission(this, 'PERM_SHOW_ALL_REQ')") public List getAll() { - Query q = dao.getQuery("from " + dao.getEntityName() + " as tr join fetch tr.ownedBy where kind = :kind order by tr.numser"); - q.setParameter("kind", Constants.REQ_TYPE_MATERIAL); - return q.list(); + hqlFilter.setDataClass(Requirement.class); + hqlFilter.andFilter("kind = :kind"); + hqlFilter.setParam("kind", Constants.REQ_TYPE_MATERIAL); + return super.getAll(); } } diff --git a/src/main/java/info/bukova/isspst/services/requirement/ReqServicesServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/ReqServicesServiceImpl.java index bafd2fb5..4bbad54f 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/ReqServicesServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/ReqServicesServiceImpl.java @@ -3,17 +3,14 @@ package info.bukova.isspst.services.requirement; import info.bukova.isspst.Constants; import info.bukova.isspst.data.Requirement; import info.bukova.isspst.data.RequirementState; -import info.bukova.isspst.data.Workgroup; - -import java.util.Date; -import java.util.List; - -import org.hibernate.Query; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PostFilter; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; +import java.util.Date; +import java.util.List; + public class ReqServicesServiceImpl extends RequirementServiceImpl implements RequirementService, RequirementBaseService { @Autowired @@ -38,10 +35,10 @@ public class ReqServicesServiceImpl extends RequirementServiceImpl implements Re @PreAuthorize("hasPermission(this, 'PERM_READ')") public List getMy() { - Query q = dao.getQuery("from " + dao.getEntityName() + " where ownedBy = :owner and kind = :kind"); - q.setParameter("owner", getLoggedInUser()); - q.setParameter("kind", Constants.REQ_TYPE_SERVICES); - return q.list(); + hqlFilter.setDataClass(Requirement.class); + hqlFilter.andFilter("kind = :kind"); + hqlFilter.setParam("kind", Constants.REQ_TYPE_SERVICES); + return super.getMy(); } @SuppressWarnings("unchecked") @@ -51,11 +48,10 @@ public class ReqServicesServiceImpl extends RequirementServiceImpl implements Re @PostFilter("hasPermission(filterObject, 'PERM_SHOW_CENTRE_REQ')") public List getCentreReq() { - List wgList = workgroupService.getUserCentres(getLoggedInUser()); - Query q = dao.getQuery("select tr from " + dao.getEntityName() + " tr join fetch tr.ownedBy join tr.centre c where c in (:wgList) and kind = :kind order by tr.numser"); - q.setParameterList("wgList", wgList); - q.setParameter("kind", Constants.REQ_TYPE_SERVICES); - return q.list(); + hqlFilter.setDataClass(Requirement.class); + hqlFilter.andFilter("kind = :kind"); + hqlFilter.setParam("kind", Constants.REQ_TYPE_SERVICES); + return super.getCentreReq(); } @SuppressWarnings("unchecked") @@ -65,13 +61,10 @@ public class ReqServicesServiceImpl extends RequirementServiceImpl implements Re @PostFilter("hasPermission(filterObject, 'PERM_SHOW_WORKGROUP_REQ')") public List getWorkgroupReq() { - List wgList = workgroupService.getUserWorkgroups(getLoggedInUser()); - Query q = dao.getQuery("select tr from " - + dao.getEntityName() - + " tr join fetch tr.ownedBy join tr.workgroup w where w in (:wgList) and kind = :kind order by tr.numser"); - q.setParameterList("wgList", wgList); - q.setParameter("kind", Constants.REQ_TYPE_SERVICES); - return q.list(); + hqlFilter.setDataClass(Requirement.class); + hqlFilter.andFilter("kind = :kind"); + hqlFilter.setParam("kind", Constants.REQ_TYPE_SERVICES); + return super.getWorkgroupReq(); } @SuppressWarnings("unchecked") @@ -80,8 +73,9 @@ public class ReqServicesServiceImpl extends RequirementServiceImpl implements Re @PreAuthorize("hasPermission(this, 'PERM_SHOW_ALL_REQ')") public List getAll() { - Query q = dao.getQuery("from " + dao.getEntityName() + " as tr join fetch tr.ownedBy where kind = :kind order by tr.numser"); - q.setParameter("kind", Constants.REQ_TYPE_SERVICES); - return q.list(); + hqlFilter.setDataClass(Requirement.class); + hqlFilter.andFilter("kind = :kind"); + hqlFilter.setParam("kind", Constants.REQ_TYPE_SERVICES); + return super.getAll(); } } diff --git a/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java index 2db68aac..2245b5be 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementBaseServiceImpl.java @@ -24,9 +24,11 @@ import info.bukova.isspst.reporting.ParamFiller; import info.bukova.isspst.reporting.Report; import info.bukova.isspst.reporting.ReportDefinition; import info.bukova.isspst.services.AbstractOwnedService; +import info.bukova.isspst.services.HqlDataFilter; import info.bukova.isspst.services.LazyLoader; import info.bukova.isspst.services.Service; import info.bukova.isspst.services.settings.GlobalSettingsService; +import info.bukova.isspst.services.settings.SeasonService; import info.bukova.isspst.services.signeddocs.SignedDocumentService; import info.bukova.isspst.services.users.UserService; import info.bukova.isspst.services.workgroups.WorkgroupService; @@ -82,6 +84,9 @@ public abstract class RequirementBaseServiceImpl exte @Autowired private ReportFileStorage signedDocStorage; + @Autowired + private SeasonService seasonService; + @Override @Transactional @PreAuthorize("hasPermission(this, 'PERM_ADD')") @@ -433,9 +438,10 @@ public abstract class RequirementBaseServiceImpl exte @PreAuthorize("hasPermission(this, 'PERM_READ')") public List getMy() { - Query q = dao.getQuery("from " + dao.getEntityName() + " where ownedBy = :owner"); - q.setParameter("owner", getLoggedInUser()); - return q.list(); + Query q = dao.getQuery("from " + dao.getEntityName()); + HqlDataFilter f = new HqlDataFilter("ownedBy = :owner"); + f.setParam("owner", getLoggedInUser()); + return filterQuery(q, f).list(); } @SuppressWarnings("unchecked") @@ -446,9 +452,10 @@ public abstract class RequirementBaseServiceImpl exte public List getCentreReq() { List wgList = workgroupService.getUserCentres(getLoggedInUser()); - Query q = dao.getQuery("select tr from " + dao.getEntityName() + " tr join fetch tr.ownedBy join tr.centre c where c in (:wgList) order by tr.numser"); - q.setParameterList("wgList", wgList); - return q.list(); + Query q = dao.getQuery("select tr from " + dao.getEntityName() + " tr join fetch tr.ownedBy join tr.centre c"); + HqlDataFilter f = new HqlDataFilter("c in (:wgList)", "tr.numser desc"); + f.setParam("wgList", wgList); + return filterQuery(q, f).list(); } @SuppressWarnings("unchecked") @@ -459,9 +466,10 @@ public abstract class RequirementBaseServiceImpl exte public List getWorkgroupReq() { List wgList = workgroupService.getUserWorkgroups(getLoggedInUser()); - Query q = dao.getQuery("select tr from " + dao.getEntityName() + " tr join fetch tr.ownedBy join tr.workgroup w where w in (:wgList) order by tr.numser"); - q.setParameterList("wgList", wgList); - return q.list(); + Query q = dao.getQuery("select tr from " + dao.getEntityName() + " tr join fetch tr.ownedBy join tr.workgroup w"); + HqlDataFilter f = new HqlDataFilter("w in (:wgList)", "tr.numser desc"); + f.setParam("wgList", wgList); + return filterQuery(q, f).list(); } @SuppressWarnings("unchecked") @@ -470,8 +478,9 @@ public abstract class RequirementBaseServiceImpl exte @PreAuthorize("hasPermission(this, 'PERM_SHOW_ALL_REQ')") public List getAll() { - Query q = dao.getQuery("from " + dao.getEntityName() + " as tr join fetch tr.ownedBy order by tr.numser"); - return q.list(); + Query q = dao.getQuery("from " + dao.getEntityName() + " as tr join fetch tr.ownedBy"); + HqlDataFilter f = new HqlDataFilter("", "tr.numser desc"); + return filterQuery(q, f).list(); } @Override diff --git a/src/main/java/info/bukova/isspst/services/settings/SeasonException.java b/src/main/java/info/bukova/isspst/services/settings/SeasonException.java new file mode 100644 index 00000000..3947b7e8 --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/settings/SeasonException.java @@ -0,0 +1,18 @@ +package info.bukova.isspst.services.settings; + +import info.bukova.isspst.services.IsspstException; + +/** + * @author Pepa Rokos + */ +public class SeasonException extends IsspstException { + + SeasonException() { + super(); + } + + SeasonException(String msg) { + super(msg); + setReason(msg); + } +} diff --git a/src/main/java/info/bukova/isspst/services/settings/SeasonService.java b/src/main/java/info/bukova/isspst/services/settings/SeasonService.java new file mode 100644 index 00000000..8031876f --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/settings/SeasonService.java @@ -0,0 +1,16 @@ +package info.bukova.isspst.services.settings; + +import info.bukova.isspst.data.Season; +import info.bukova.isspst.services.Service; + +import java.util.List; + +/** + * @author Pepa Rokos + */ +public interface SeasonService extends Service { + Season addSeason(); + Season getActive(); + List getAllSeasons(); + void setActive(Season season); +} diff --git a/src/main/java/info/bukova/isspst/services/settings/SeasonServiceImpl.java b/src/main/java/info/bukova/isspst/services/settings/SeasonServiceImpl.java new file mode 100644 index 00000000..d63a0759 --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/settings/SeasonServiceImpl.java @@ -0,0 +1,100 @@ +package info.bukova.isspst.services.settings; + +import info.bukova.isspst.StringUtils; +import info.bukova.isspst.data.NumberSeries; +import info.bukova.isspst.data.Season; +import info.bukova.isspst.services.AbstractOwnedService; +import info.bukova.isspst.services.numberseries.NumberSeriesService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; +import java.util.List; + +/** + * @author Pepa Rokos + */ +public class SeasonServiceImpl extends AbstractOwnedService implements SeasonService { + private Season activeSeason; + @Autowired + private NumberSeriesService numberSeriesService; + + @Transactional + @Override + public Season addSeason() { + Season season = new Season(); + season.setDescription(StringUtils.localize("SeasonsNewSeason")); + season.setValidFrom(new Date()); + season.setActive(true); + + Season last = selectSingle("from Season where validTo Is Null"); + add(season); + + last.setValidTo(new Date()); + last.setActive(false); + update(last); + + + activeSeason = null; + + List numSeries = numberSeriesService.getSeriesForSeason(last); + for (NumberSeries ns: numSeries) { + String txt = ns.getPrefix().replaceAll("[0-9]+", ""); + String numTxt = ns.getPrefix().replaceAll("[a-zA-Z]+", ""); + + int num = 0; + try { + num = Integer.valueOf(numTxt); + } catch (Exception e) { + e.printStackTrace(); + } + + String newPrefix = txt + String.valueOf(num + 1); + + NumberSeries newNumSer = new NumberSeries(); + newNumSer.setPrefix(newPrefix); + newNumSer.setModule(ns.getModule()); + newNumSer.setNumber(1); + newNumSer.setSeason(season); + numberSeriesService.add(newNumSer); + } + return season; + } + + @Transactional + @Override + public Season getActive() { + if (activeSeason == null) { + activeSeason = selectSingle("from Season where active = true"); + } + + if (activeSeason == null) { + throw new SeasonException("Active_season_not_set"); + } + return activeSeason; + } + + @Override + @Transactional + public List getAllSeasons() { + return getAll(); + } + + @Transactional + @Override + public void setActive(Season season) { + for (Season s: getAll()) { + s.setActive(false); + update(s); + } + + Season s = dao.getById(season.getId()); + + if (s == null) { + throw new SeasonException("Season_not_in_database"); + } + + s.setActive(true); + activeSeason = s; + } +} diff --git a/src/main/java/info/bukova/isspst/services/tripbill/TripBillServiceImpl.java b/src/main/java/info/bukova/isspst/services/tripbill/TripBillServiceImpl.java index c6e3510e..de09e16a 100644 --- a/src/main/java/info/bukova/isspst/services/tripbill/TripBillServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/tripbill/TripBillServiceImpl.java @@ -6,6 +6,7 @@ import info.bukova.isspst.data.TripBill; import info.bukova.isspst.data.TripBillItem; import info.bukova.isspst.data.TripRequirement; import info.bukova.isspst.services.AbstractOwnedService; +import info.bukova.isspst.services.HqlDataFilter; import info.bukova.isspst.services.LazyLoader; import info.bukova.isspst.services.settings.GlobalSettingsService; import org.hibernate.Hibernate; @@ -179,9 +180,10 @@ public class TripBillServiceImpl extends AbstractOwnedService implemen @Transactional @PreAuthorize("hasPermission(this, 'PERM_READ')") public List getMy() { - Query q = dao.getQuery("from TripBill where ownedBy = :owner"); - q.setParameter("owner", getLoggedInUser()); - return q.list(); + Query q = dao.getQuery("from TripBill"); + HqlDataFilter f = new HqlDataFilter("ownedBy = :owner"); + f.setParam("owner", getLoggedInUser()); + return filterQuery(q, f).list(); } @Override diff --git a/src/main/java/info/bukova/isspst/ui/ListViewModel.java b/src/main/java/info/bukova/isspst/ui/ListViewModel.java index 1d63fe89..c722523f 100644 --- a/src/main/java/info/bukova/isspst/ui/ListViewModel.java +++ b/src/main/java/info/bukova/isspst/ui/ListViewModel.java @@ -2,9 +2,13 @@ package info.bukova.isspst.ui; import info.bukova.isspst.StringUtils; import info.bukova.isspst.data.DataModel; +import info.bukova.isspst.data.Season; +import info.bukova.isspst.data.SeasonsAware; import info.bukova.isspst.filters.Filter; +import info.bukova.isspst.services.HqlDataFilter; import info.bukova.isspst.services.IsspstException; import info.bukova.isspst.services.Service; +import info.bukova.isspst.services.settings.SeasonService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.DataIntegrityViolationException; @@ -18,6 +22,7 @@ import org.zkoss.bind.annotation.NotifyChange; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; +import org.zkoss.zk.ui.select.annotation.WireVariable; import org.zkoss.zul.Messagebox; import org.zkoss.zul.Window; @@ -44,6 +49,13 @@ public class ListViewModel extends DocumentViewModel private boolean newRec = false; private boolean fullFill = false; + @WireVariable + private SeasonService seasonService; + private List seasons; + private Season selSeason; + @WireVariable + private HqlDataFilter hqlFilter; + protected Service service; protected Class dataClass; protected String formZul; @@ -53,6 +65,13 @@ public class ListViewModel extends DocumentViewModel public void initListViewModel() { super.initDocumentViewModel(); + seasons = seasonService.getAllSeasons(); + + Map map = Executions.getCurrent().getParameterMap(); + if (map.get("select") == null) { + selSeason = seasonService.getActive(); + } + setHqlFilter(); } public List getDataList() { @@ -303,6 +322,9 @@ public class ListViewModel extends DocumentViewModel } if (id > 0) { + selSeason = null; + setHqlFilter(); + dataList = getListFromService(); for (int i = 0; i < dataList.size(); i++) { if (dataList.get(i).getId() == id) { selIndex = i; @@ -397,11 +419,61 @@ public class ListViewModel extends DocumentViewModel public boolean isAbleToDelete() { + if (isSeasonFilter() + &&(!seasonService.getActive().equals(selSeason))) { + return false; + } return this.isRecordSelected(); } public boolean isAbleToAdd() { + if (isSeasonFilter() + &&(!seasonService.getActive().equals(selSeason))) { + return false; + } return true; } + + public List getSeasons() { + return seasons; + } + + public Season getSelSeason() { + return selSeason; + } + + public void setSelSeason(Season selSeason) { + this.selSeason = selSeason; + } + + private void setHqlFilter() { + if (selSeason == null) { + hqlFilter.reset(); + } else { + hqlFilter.setWhere("SEASON_ID = :season"); + hqlFilter.setParam("season", selSeason.getId()); + hqlFilter.setDataClass(SeasonsAware.class); + } + } + + @Command + @NotifyChange({"dataList", "ableToAdd", "selIndex", "dataBean", "ableToDelete"}) + public void filterSeason() { + setHqlFilter(); + dataList.clear(); + loadFromDbSync(); + dataBean = null; + selIndex = -1; + } + + public boolean isSeasonFilter() { + for (Class clazz : service.getEntityClasses()) { + if (SeasonsAware.class.isAssignableFrom(clazz)) { + return true; + } + } + + return false; + } } diff --git a/src/main/java/info/bukova/isspst/ui/MainMenu.java b/src/main/java/info/bukova/isspst/ui/MainMenu.java index df631dce..4a86eaeb 100644 --- a/src/main/java/info/bukova/isspst/ui/MainMenu.java +++ b/src/main/java/info/bukova/isspst/ui/MainMenu.java @@ -44,7 +44,7 @@ public class MainMenu @Command public void numSeries() { - Window window = (Window) Executions.createComponents("/settings/numberSeries.zul", null, null); + Window window = (Window) Executions.createComponents("/settings/seasons.zul", null, null); window.doModal(); } diff --git a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementList.java b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementList.java index 98eb7aa0..2a09f51f 100644 --- a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementList.java +++ b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementList.java @@ -30,7 +30,7 @@ public class TripRequirementList extends ListViewModel { private boolean showWorkgroup; private boolean showAll; - @Init + @Init(superclass = true) public void init() { service = tripRequirementService; dataClass = TripRequirement.class; diff --git a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListAll.java b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListAll.java index 266bcc5f..b55a1bc1 100644 --- a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListAll.java +++ b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListAll.java @@ -33,7 +33,7 @@ public class TripRequirementListAll extends RequirementSubpage private List allCentres; private List allWorkgroups; - @Init + @Init(superclass = true) public void init() { service = tripRequirementService; dataClass = TripRequirement.class; diff --git a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListCentre.java b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListCentre.java index ba6b9a71..e3224971 100644 --- a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListCentre.java +++ b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListCentre.java @@ -35,7 +35,7 @@ public class TripRequirementListCentre extends RequirementSubpage myCentres; - @Init + @Init(superclass = true) public void init() { service = tripRequirementService; dataClass = TripRequirement.class; diff --git a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListWorkgroup.java b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListWorkgroup.java index 30f91122..177f9c14 100644 --- a/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListWorkgroup.java +++ b/src/main/java/info/bukova/isspst/ui/requirement/TripRequirementListWorkgroup.java @@ -37,7 +37,7 @@ public class TripRequirementListWorkgroup extends RequirementSubpage myWorkgroups; - @Init + @Init(superclass = true) public void init() { service = tripRequirementService; dataClass = TripRequirement.class; diff --git a/src/main/java/info/bukova/isspst/ui/settings/SeasonsVM.java b/src/main/java/info/bukova/isspst/ui/settings/SeasonsVM.java new file mode 100644 index 00000000..8f0846e3 --- /dev/null +++ b/src/main/java/info/bukova/isspst/ui/settings/SeasonsVM.java @@ -0,0 +1,101 @@ +package info.bukova.isspst.ui.settings; + +import info.bukova.isspst.Module; +import info.bukova.isspst.ModuleUtils; +import info.bukova.isspst.StringUtils; +import info.bukova.isspst.data.NumberSeries; +import info.bukova.isspst.data.Season; +import info.bukova.isspst.services.numberseries.NumberSeriesService; +import info.bukova.isspst.services.settings.SeasonService; +import org.zkoss.bind.BindUtils; +import org.zkoss.bind.annotation.BindingParam; +import org.zkoss.bind.annotation.Command; +import org.zkoss.bind.annotation.Init; +import org.zkoss.bind.annotation.NotifyChange; +import org.zkoss.zk.ui.event.Event; +import org.zkoss.zk.ui.event.EventListener; +import org.zkoss.zk.ui.select.annotation.WireVariable; +import org.zkoss.zul.Messagebox; +import org.zkoss.zul.Window; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author Pepa Rokos + */ +public class SeasonsVM { + @WireVariable + private SeasonService seasonService; + @WireVariable + private NumberSeriesService numericSeriesService; + private List seasons; + private List numSeries; + private Map moduleMap; + + @Init + public void init() { + seasons = seasonService.getAll(); + numSeries = numericSeriesService.getSeriesForSeason(seasonService.getActive()); + moduleMap = new HashMap(); + for (Module m : ModuleUtils.getActiveModules()) { + moduleMap.put(m.getId(), m); + } + } + + public Map getModuleMap() { + return moduleMap; + } + + @Command + //@NotifyChange({"seasons", "numSeries"}) + public void addSeason() { + Messagebox.show(StringUtils.localize("SeasonsNewSeasonDialog"), StringUtils.localize("SeasonsNewSeason"), Messagebox.YES | Messagebox.NO, Messagebox.QUESTION, + new EventListener() { + @Override + public void onEvent(Event event) throws Exception { + if (((Integer)event.getData()).intValue() == Messagebox.YES) { + Season newSeason = seasonService.addSeason(); + seasons = seasonService.getAll(); + numSeries = numericSeriesService.getSeriesForSeason(seasonService.getActive()); + BindUtils.postNotifyChange(null, null, SeasonsVM.this, "seasons"); + BindUtils.postNotifyChange(null, null, SeasonsVM.this, "numSeries"); + } + } + }); + } + + @Command + public void save(@BindingParam("window") Window win) { + for(Season s: seasons) { + seasonService.update(s); + } + + for(NumberSeries n: numSeries) { + numericSeriesService.update(n); + } + + win.detach(); + } + + @Command + @NotifyChange({"numSeries", "seasons"}) + public void activate(@BindingParam("season") Season season) { + seasonService.setActive(season); + seasons = seasonService.getAll(); + numSeries = numericSeriesService.getSeriesForSeason(season); + } + + public List getSeasons() { + return seasons; + } + + public List getNumSeries() { + return numSeries; + } + + public boolean isCanSave() { + return true; + } +} diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index 1ad512cf..4b8daf67 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -38,5 +38,6 @@ + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/locales/zk-label.properties b/src/main/webapp/WEB-INF/locales/zk-label.properties index c5a76254..4a43b1a4 100644 --- a/src/main/webapp/WEB-INF/locales/zk-label.properties +++ b/src/main/webapp/WEB-INF/locales/zk-label.properties @@ -431,3 +431,16 @@ ContextMenu = Volby v kontextovém menu ErrMaterialOrServiceDescription=Zadejte popis požadavku. +SeasonsSeason = Období +SeasonsNewSeason = Nové období +SeasonsDescription = Popis +SeasonsValidFrom = Platí od +SeasonsValidTo = Platí do +SeasonsActive = Aktivní +SeasonsActivate = Aktivovat +SeasonsActiveNumSeries = Číselné řady pro aktivní období: +SeasonsAgenda = Agenda +SeasonsPrefix = Prefix +SeasonsNewSeasonDialog = Opravdu chcete založit nové období? +Active_season_not_set = Není nastaveno žádné aktivní období. + diff --git a/src/main/webapp/WEB-INF/spring/root-context.xml b/src/main/webapp/WEB-INF/spring/root-context.xml index bfc9018f..7647dbf5 100644 --- a/src/main/webapp/WEB-INF/spring/root-context.xml +++ b/src/main/webapp/WEB-INF/spring/root-context.xml @@ -191,6 +191,10 @@ + + + + @@ -280,6 +284,10 @@ + + + + @@ -466,4 +474,8 @@ + + + + diff --git a/src/main/webapp/app/toolbar.zul b/src/main/webapp/app/toolbar.zul index fca0ddb6..2baea222 100644 --- a/src/main/webapp/app/toolbar.zul +++ b/src/main/webapp/app/toolbar.zul @@ -6,5 +6,14 @@ +