Refactor SimpleWebApp in two projects

This commit is contained in:
nheron 2014-02-14 16:56:27 +01:00
commit 4d7a77d2b5
26 changed files with 180 additions and 57 deletions

View file

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.chtijbug.drools:drools-framework-runtime-base:2.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: org.chtijbug.drools:drools-framework-common:2.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.5" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.2" level="project" />
<orderEntry type="library" name="Maven: commons-beanutils:commons-beanutils:1.8.3" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.1" level="project" />
<orderEntry type="library" name="Maven: commons-collections:commons-collections:3.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-io:1.3.2" level="project" />
<orderEntry type="library" name="Maven: org.drools:drools-compiler:5.6.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.drools:drools-core:5.6.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.mvel:mvel2:2.1.8.Final" level="project" />
<orderEntry type="library" name="Maven: org.drools:knowledge-api:5.6.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.drools:knowledge-internal-api:5.6.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.antlr:antlr-runtime:3.3" level="project" />
<orderEntry type="library" name="Maven: org.antlr:antlr:3.3" level="project" />
<orderEntry type="library" name="Maven: org.antlr:stringtemplate:3.2.1" level="project" />
<orderEntry type="library" name="Maven: antlr:antlr:2.7.7" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jdt.core.compiler:ecj:3.5.1" level="project" />
<orderEntry type="library" name="Maven: com.thoughtworks.xstream:xstream:1.4.3" level="project" />
<orderEntry type="library" name="Maven: xmlpull:xmlpull:1.1.3.1" level="project" />
<orderEntry type="library" name="Maven: xpp3:xpp3_min:1.1.4c" level="project" />
<orderEntry type="library" name="Maven: org.jbpm:jbpm-bpmn2:5.5.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.jbpm:jbpm-flow-builder:5.5.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.jbpm:jbpm-flow:5.5.0.Final" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:2.5.0" level="project" />
<orderEntry type="library" name="Maven: org.chtijbug.drools:drools-framework-runtime-entity:2.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jettison:jettison:1.3" level="project" />
<orderEntry type="library" name="Maven: stax:stax-api:1.0.1" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:15.0" level="project" />
</component>
</module>

View file

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>droolscourse</artifactId>
<groupId>com.pymma-software.droolscourse</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>droolscourse-service</artifactId>
<dependencies>
<dependency>
<groupId>org.chtijbug.drools</groupId>
<artifactId>drools-framework-runtime-base</artifactId>
</dependency>
<dependency>
<groupId>org.chtijbug.drools</groupId>
<artifactId>drools-framework-runtime-entity</artifactId>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,55 @@
package loyalty;
import loyalty.domains.Ticket;
import org.chtijbug.drools.runtime.DroolsChtijbugException;
import org.chtijbug.drools.runtime.RuleBaseBuilder;
import org.chtijbug.drools.runtime.RuleBasePackage;
import org.chtijbug.drools.runtime.RuleBaseSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import util.CoursHistoryContainer;
public class DroolsServiceCalculate implements IDroolsServiceCalculate {
/*
* (non-Javadoc)
*
* @see
* IDroolsServiceCalculate#calculate(droolscours.loyalty.domains
* .Ticket)
*/
/**
* Class Logger
*/
private static Logger logger = LoggerFactory.getLogger(DroolsServiceCalculate.class);
private RuleBasePackage ruleBasePackage = null;
@Override
public Ticket calculate( Ticket ticket) {
if (ruleBasePackage == null) {
try {
CoursHistoryContainer coursHistoryContainer = new CoursHistoryContainer();
ruleBasePackage= RuleBaseBuilder.createPackageBasePackageWithListener(coursHistoryContainer, "File1.drl");
//ruleBasePackage= RuleBaseBuilder.createGuvnorRuleBasePackageWithListener(coursHistoryContainer,"http://localhost:8080", "drools-guvnor", "mypackage","1.27.0-PROD",
// "admin", "admin");
} catch (DroolsChtijbugException e) {
logger.error("Could not create RuleBase", e);
}
}
RuleBaseSession sessionStatefull = null;
try {
sessionStatefull = ruleBasePackage.createRuleBaseSession();
sessionStatefull.insertByReflection(ticket);
sessionStatefull.fireAllRules();
// System.out.println(sessionStatefull.getHistoryContainer().getListHistoryEvent().toString());
} catch (DroolsChtijbugException e) {
logger.error("Error in fireallrules", e);
}
return ticket;
}
}

View file

@ -0,0 +1,11 @@
package loyalty;
import loyalty.domains.Ticket;
public interface IDroolsServiceCalculate {
public abstract Ticket calculate( Ticket ticket);
}

View file

@ -0,0 +1,59 @@
package loyalty.domains;
public class Card {
private String number;
private String name;
private Customer customer;
public void setNumber(String number) {
this.number = number;
}
public void setName(String name) {
this.name = name;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public void setCartType(String cartType) {
this.cartType = cartType;
}
private String cartType;
public Card(String number, String cartName, String cartType,
String customerID, String name, String surname, String maritalName,
Gender gender) {
this.number = number;
this.name = cartName;
this.cartType = cartType;
this.customer = new Customer(customerID, name, surname, maritalName,
gender, null);
}
public String getName() {
return name;
}
public String getCartType() {
return cartType;
}
public String getNumber() {
return number;
}
public Customer getCustomer() {
return customer;
}
public Card() {
super();
// TODO Auto-generated constructor stub
}
}

View file

@ -0,0 +1,5 @@
package loyalty.domains;
public enum Currency {
Euro, Dollar, Yen
}

View file

@ -0,0 +1,77 @@
package loyalty.domains;
import java.util.Date;
public class Customer {
private String customerID;
private String surName;
private String name;
private String maritalName;
private Gender gender;
private Date birthDate;
public String getCustomerID() {
return customerID;
}
public void setCustomerID(String customerID) {
this.customerID = customerID;
}
public String getSurName() {
return surName;
}
public void setSurName(String surName) {
this.surName = surName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMaritalName() {
return maritalName;
}
public void setMaritalName(String maritalName) {
this.maritalName = maritalName;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public Customer(String customerID, String surName, String name,
String maritalName, Gender gender, Date birthDate) {
super();
this.customerID = customerID;
this.surName = surName;
this.name = name;
this.maritalName = maritalName;
this.gender = gender;
this.birthDate = birthDate;
}
public Customer() {
super();
// TODO Auto-generated constructor stub
}
}

View file

@ -0,0 +1,5 @@
package loyalty.domains;
public enum Gender {
Mr,Mrs,Miss
}

View file

@ -0,0 +1,23 @@
package loyalty.domains;
public class Ligneop {
private String nomOP;
private Float montantReduction;
public String getNomOP() {
return nomOP;
}
public void setNomOP(String nomOP) {
this.nomOP = nomOP;
}
public Float getMontantReduction() {
return montantReduction;
}
public void setMontantReduction(Float montantReduction) {
this.montantReduction = montantReduction;
}
public Ligneop() {
super();
// TODO Auto-generated constructor stub
}
}

View file

@ -0,0 +1,35 @@
package loyalty.domains;
public class Price {
private Float price;
private Currency currency;
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public Currency getCurrency() {
return currency;
}
public void setCurrency(Currency currency) {
this.currency = currency;
}
public Price(Float price, Currency currency) {
super();
this.price = price;
this.currency = currency;
}
public Price() {
super();
// TODO Auto-generated constructor stub
}
}

View file

@ -0,0 +1,55 @@
package loyalty.domains;
public class Product {
private String Id;
private String Name;
private Price price;
public Price getPrice() {
return price;
}
public void setPrice(Price price) {
this.price = price;
}
private Provider provider;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Provider getProvider() {
return provider;
}
public void setProvider(Provider provider) {
this.provider = provider;
}
public Product(String id, String name, Provider provider) {
super();
Id = id;
Name = name;
this.provider = provider;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
}

View file

@ -0,0 +1,34 @@
package loyalty.domains;
public class Provider {
private String name;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public Provider(String name, String country) {
super();
this.name = name;
this.country = country;
}
public Provider() {
super();
// TODO Auto-generated constructor stub
}
}

View file

@ -0,0 +1,85 @@
package loyalty.domains;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Ticket {
private String Id;
private Date dateTicket;
private Float amount;
private Card loyaltyCard;
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Card getLoyaltyCard() {
return loyaltyCard;
}
public void setLoyaltyCard(Card loyaltyCard) {
this.loyaltyCard = loyaltyCard;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public Date getDateTicket() {
return dateTicket;
}
public void setDateTicket(Date date) {
this.dateTicket = date;
}
private List<TicketLine> ticketLines = new ArrayList<TicketLine>();
public long AddLine(Product product, Float price, long quantity) {
TicketLine newLine = new TicketLine(this, product, quantity, price);
ticketLines.add(newLine);
this.amount = this.amount + newLine.getLineTotal();
return newLine.getLineNumber();
}
public void delLine(int lineNumber) {
if (lineNumber <= this.ticketLines.size()) {
TicketLine lineToDel = this.ticketLines.get(lineNumber);
lineToDel.setValid(false);
this.amount = this.amount-lineToDel.getLineTotal();
}
}
public Ticket() {
super();
// TODO Auto-generated constructor stub
}
public Float getAmount() {
return amount;
}
public void setAmount(Float amount) {
this.amount = amount;
}
public List<TicketLine> getTicketLines() {
return ticketLines;
}
public void setTicketLines(List<TicketLine> ticketLines) {
this.ticketLines = ticketLines;
}
}

View file

@ -0,0 +1,121 @@
package loyalty.domains;
public class TicketLine {
private static int numberLines = 0;
private String ticketID;
private Ticket ticket;
private int lineNumber;
private String productID;
private Product product;
private long quantity;
private Float price = null;
private Float lineTotal;;
private Ligneop op;
public Ligneop getOp() {
return op;
}
public String getProductID() {
return productID;
}
public void setProductID(String productID) {
this.productID = productID;
}
public String getTicketID() {
return this.ticketID;
}
public void setTicketID(String ticketID) {
this.ticketID = ticketID;
}
public void setOp(Ligneop op) {
this.op = op;
}
private boolean valid;
public int getLineNumber() {
return lineNumber;
}
public Float getLineTotal() {
return lineTotal;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
public Ticket getTicket() {
return ticket;
}
public void setTicket(Ticket ticket) {
this.ticket = ticket;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public long getQuantity() {
return quantity;
}
public TicketLine(Ticket ticket, Product product, long quantity, Float price) {
super();
numberLines = numberLines + 1;
this.lineNumber = numberLines;
this.ticket = ticket;
this.product = product;
this.quantity = quantity;
this.price = price;
this.lineTotal = price * quantity;
}
public void setQuantity(long quantity) {
this.quantity = quantity;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public TicketLine() {
super();
// TODO Auto-generated constructor stub
}
public static int getNumberLines() {
return numberLines;
}
public static void setNumberLines(int numberLines) {
TicketLine.numberLines = numberLines;
}
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
public void setLineTotal(Float lineTotal) {
this.lineTotal = lineTotal;
}
public void AddProductToTicketLine(Product p){
this.product = p;
}
}

View file

@ -0,0 +1,26 @@
package util;
import org.chtijbug.drools.entity.history.HistoryEvent;
import org.chtijbug.drools.runtime.DroolsChtijbugException;
import org.chtijbug.drools.runtime.listener.HistoryListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by IntelliJ IDEA.
* Date: 14/02/14
* Time: 11:47
* To change this template use File | Settings | File Templates.
*/
public class CoursHistoryContainer implements HistoryListener {
/**
* Class Logger
*/
private static Logger logger = LoggerFactory.getLogger(CoursHistoryContainer.class);
@Override
public void fireEvent(HistoryEvent historyEvent) throws DroolsChtijbugException {
logger.info(historyEvent.toString());
}
}

View file

@ -0,0 +1,20 @@
package util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateHelper {
public static String sFormat = "yyyy-MM-dd";
public static Date getDate(String sDate) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
return sdf.parse(sDate);
}
public static Date getDate(String sDate, String anotherFormat)
throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(anotherFormat);
return sdf.parse(sDate);
}
}

View file

@ -0,0 +1,30 @@
package util;
import loyalty.DroolsServiceCalculate;
import loyalty.domains.Product;
import loyalty.domains.Ticket;
import loyalty.domains.TicketLine;
/**
* Created by IntelliJ IDEA.
* Date: 14/02/14
* Time: 13:12
* To change this template use File | Settings | File Templates.
*/
public class Main {
public static void main(String args[]) throws Exception{
DroolsServiceCalculate droolsServiceCalculate = new DroolsServiceCalculate();
Ticket ticket = new Ticket();
ticket.setDateTicket(DateHelper.getDate("2012-01-01"));
TicketLine ticketLine = new TicketLine();
ticket.getTicketLines().add(ticketLine);
ticketLine.setLineTotal(new Float("2.0"));
Product product = new Product();
product.setName("pampers");
ticketLine.setProduct(product);
droolsServiceCalculate.calculate(ticket);
}
}

View file

@ -0,0 +1,34 @@
package util;
import java.util.Date;
public class MyDate {
private String uneOp;
private Date uneDate1;
private Date uneDate2;
public String getUneOp() {
return uneOp;
}
public void setUneOp(String uneOp) {
this.uneOp = uneOp;
}
public Date getUneDate1() {
return uneDate1;
}
public void setUneDate1(Date uneDate1) {
this.uneDate1 = uneDate1;
}
public Date getUneDate2() {
return uneDate2;
}
public void setUneDate2(Date uneDate2) {
this.uneDate2 = uneDate2;
}
public MyDate() {
super();
// TODO Auto-generated constructor stub
}
}

View file

@ -0,0 +1,35 @@
//#created on: 21 nov. 2010
package loyalty
import util.MyDate;
import loyalty.domains.Ticket;
import loyalty.domains.TicketLine;
import loyalty.domains.Ligneop;
import loyalty.domains.Product
import util.DateHelper;
dialect "mvel"
rule "createdateOpPampers"
when
then
MyDate t = new MyDate();
t.setUneDate1(DateHelper.getDate("2010-12-01"));
t.setUneDate2(DateHelper.getDate("2014-12-31"));
t.setUneOp("pampers");
insert(t);
end
rule "opPameprs"
when
MyDate(uneOp=="pampers",$d1:uneDate1,$d2:uneDate2)
$p : Product( name=="pampers")
$l : TicketLine(product==$p)
$t : Ticket(ticketLines contains $l,dateTicket >= $d1 && dateTicket <= $d2)
then
$l.setOp(new Ligneop());
$l.getOp().setMontantReduction(new Float($l.getLineTotal()*0.2));
$l.getOp().setNomOP("opPameprs");
end