📄 serviceimpl.java
字号:
package service;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import util.Theme;
import dao.DAOException;
import dao.DaoFactory;
import dao.model.Categorie;
import dao.model.Contrat;
import dao.model.Passage;
import dao.model.Plage;
import dao.model.Spot;
import exception.MetieException;
public class ServiceImpl implements IService {
public static final String PLAGES_INSUFFISSANT = "plages insuffissant";
@Override
public void addCategorie(Categorie c) throws MetieException {
try {
DaoFactory.getCategorieDao().insert(c);
} catch (DAOException e) {
e.printStackTrace();
throw new MetieException();
}
}
@Override
public void addContratComplete(Contrat c, Map<Plage, Timestamp> map) {
try {
int contratId = DaoFactory.getContratDao().generateIdForContrat();
int spotId = DaoFactory.getSpotDao().generateIdForSpot();
c.getSpot().setId(spotId);
DaoFactory.getSpotDao().insert(c.getSpot());
c.setDate(new Date(Calendar.getInstance().getTimeInMillis()));
Set<Plage> set=map.keySet();
for (Plage p : set) {
Timestamp t=map.get(p);
attachSpotToPlage(c.getSpot(), p,t);
}
if (c.getNomClient().length() > 8) {
c.setId(c.getNomClient().substring(0, 7)
+ (c.getDate().getYear()+1900) + contratId);
} else {
c.setId(c.getNomClient() + (c.getDate().getYear()+1900) + contratId);
}
DaoFactory.getContratDao().insert(c);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void attachSpotToPlage(Spot s, Plage p,Timestamp debut) throws MetieException {
try {
DaoFactory.getSpotDao().attachSpotToPlage(s.getId(), p.getId(), debut);
p.setTemprest(p.getTemprest() - s.getDuree());
DaoFactory.getPlageDao().update(p);
} catch (DAOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public Categorie getCategorie(String nom) throws MetieException {
Categorie c = new Categorie();
try {
c = DaoFactory.getCategorieDao().rechercher(nom);
} catch (DAOException e) {
throw new MetieException();
}
return c;
}
@Override
public List<Contrat> getContrat(String like) throws MetieException {
List<Contrat> list = new ArrayList<Contrat>();
try {
list = DaoFactory.getContratDao().rechercher(like);
} catch (DAOException e) {
e.printStackTrace();
throw new MetieException();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public List<Categorie> getListCategorie() throws MetieException {
List<Categorie> cList = new ArrayList<Categorie>();
try {
cList = DaoFactory.getCategorieDao().listage();
} catch (DAOException e) {
e.printStackTrace();
throw new MetieException();
}
return cList;
}
public Map<Plage, Timestamp> getPlageByContrat(int idSpot)
throws MetieException {
try {
return DaoFactory.getPlageDao().rechercher(idSpot);
} catch (DAOException d) {
d.printStackTrace();
throw new MetieException();
}
}
@Override
public List<Plage> getPlagesByJour(Date date) throws MetieException {
try {
return DaoFactory.getPlageDao().getListPlageByJour(date);
} catch (DAOException e) {
e.printStackTrace();
return null;
}
}
@Override
public List<Spot> getSpotsByPlage(int idPlage) throws MetieException {
try {
return DaoFactory.getSpotDao().getSpotsByPlageId(idPlage);
} catch (DAOException e) {
e.printStackTrace();
return null;
}
}
@Override
public Map<Categorie, List<Plage>> suggestPlageParticulier(int duree,Date start,Date end) throws MetieException{
Map<Categorie, List<Plage>> map=new HashMap<Categorie, List<Plage>>();
try {
List<Categorie> listCate=DaoFactory.getCategorieDao().listage();
List<Plage> listPlage = DaoFactory.getPlageDao().getPlagesParticulier(duree, start, end);
for(Categorie c: listCate){
List<Plage> suggestPlages=new ArrayList<Plage>();
for(Plage p:listPlage){
if(p.getCategorie().equals(c.getNom())){
suggestPlages.add(p);
}
}
map.put(c, suggestPlages);
}
return map;
} catch (DAOException e) {
e.printStackTrace();
throw new MetieException("fail in suggest plages particulier");
}
}
@Override
public Map<Categorie, List<Plage>> suggestPlageBySpot(Spot s)
throws MetieException {
Map<Categorie, List<Plage>> suggestMap = new HashMap<Categorie, List<Plage>>();
for (Passage p : s.getLesPassages()) {
List<Plage> suggestList = new ArrayList<Plage>();
Categorie categorie = getCategorie(p.getCategorie());
try {
Date miniDate = null;
if (s.getMiniDate() != null) {
miniDate = s.getMiniDate();
} else {
miniDate = new Date(new java.util.Date().getTime());
}
List<Plage> list = DaoFactory.getPlageDao().getPlagesByCritere(
p.getCategorie(), miniDate);
for (Plage plage : list) {
if (plage.getTemprest() < s.getDuree()) {
continue;
}
List<Spot> listSpots = DaoFactory.getSpotDao()
.getSpotsByPlageId(plage.getId());
plage.setLesSpots(listSpots);
Theme theme = new Theme();
int i = listSpots.size();
int j = i - 2;
if (listSpots != null && i > 0) {
String themeWait = s.getTheme();
String themePre = listSpots.get(--i).getTheme();
if (theme.isCompatible(themeWait, themePre)) {
boolean flag = true;
while (i-- > j && i >= 0) {
if (themeWait.equals(listSpots.get(i)
.getTheme())) {
flag = false;
break;
}
}
if (flag) {
suggestList.add(plage);
suggestMap.put(categorie, suggestList);
}
} else {
continue;
}
} else {
suggestList.add(plage);
suggestMap.put(categorie, suggestList);
}
}
if (suggestList.size() < p.getNombre()) {
throw new MetieException(PLAGES_INSUFFISSANT);
}
} catch (DAOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return suggestMap;
}
@Override
public void updateCategorie(Categorie c) throws MetieException {
try {
DaoFactory.getCategorieDao().update(c);
} catch (DAOException e) {
throw new MetieException();
}
}
@Override
public void addPlage(Plage p) throws MetieException {
try {
DaoFactory.getPlageDao().insert(p);
} catch (DAOException e) {
throw new MetieException("insert fail");
}
}
public Timestamp getMaxJournee(){
try {
return DaoFactory.getPlageDao().getMaxDate();
} catch (DAOException e) {
e.printStackTrace();
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -