Architecture Client - Serveur avec Maven
Bonjour à tous et à toutes !
Je suis actuellement en train de développer une application mobile utilisant REST ( jersey ) pour la communication Client - Serveur.
J'utilise JPA ( eclipseLink ) côté serveur, pour stocker mes informations sur une base de donnée MySQL.
Le problème que je rencontre actuellement , c'est que j'aimerais faire en sorte que l'application mobile ne possède pas les classes métier
disponible dans le package serveur. J'aimerai donc faire en sorte de proposer des classes "Lite" afin de procéder aux échanges.
Au lieu de réécrire du code bêtement, j'aimerais construire une architecture à base d'héritage afin de proposer seulement les getters pour les classes
qui vont se trouver sur Android, et tout le service CRUD pour le package côté serveur.
J'ai donc déjà construit deux packages comprenant les interfaces clientes et les interfaces serveurs.
Voici l'exemple d'une classe :
Interface Cliente :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public interface IPortalClient {
/* Location */
<K extends ILocationClient> K getLocation();
/* Inventory */
<K extends IInventoryClient> K getShop();
/* Faction */
<K extends IFactionClient> K getFaction();
/* Links */
<K extends ILinkClient> List<K> getLinks();
/* Resonators */
<K extends IResonatorClient> List<K> getResonators();
} |
Interface Serveur :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public interface IPortal extends IIdentifiable<Integer>, IPortalClient
{
/* Faction */
void setFaction(IFaction faction) ;
/* Links */
void addLinks (List<ILink> links);
void addLinks (ILink... links);
void addLink (ILink link);
void removeAllLinks ();
void removeLinkById (int id) throws SeekForIdException;
/* Resonators */
IResonator getResonatorById (int id) throws SeekForIdException;
void addResonators (List<IResonator> resonators);
void addResonators (IResonator... resonators);
void addResonator (IResonator resonator);
void deleteAllResonators ();
void deleteResonatorById (int id) throws SeekForIdException;
} |
( Avec IIdentifiable une interface générique comprenant seulement PK getId())
Au niveau des interfaces je ne pense pas avoir fais d'erreurs.
C'est au niveau de l'implémentation qu'il y a un problème.
Classe d'implémentation côté client :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
public class PortalClient implements IPortalClient {
protected IInventoryClient shop;
protected ILatLngLocationClient location;
protected IFactionClient faction;
protected List<ILinkClient> links;
protected List<IResonatorClient> resonators;
@Override
public ILatLngLocationClient getLocation() {
return location;
}
@Override
public IInventoryClient getShop() {
return shop;
}
@Override
public IFactionClient getFaction() {
return faction;
}
@Override
public List<ILinkClient> getLinks() {
return links;
}
@Override
public List<IResonatorClient> getResonators() {
return resonators;
}
} |
(Pour ne pas réécrire du code, j'aimerais utiliser l'héritage comme ci - dessous )
Classe d'implémentation côté serveur :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
@Entity
@Table(name = "PORTAL")
@XmlRootElement
@NamedQueries({
@NamedQuery(name="iportal.findAll",
query="SELECT portal FROM PortalJPA portal")})
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class PortalJPA extends PortalClient implements IPortal {
private int id;
/* - - - - - C O N S T R U C T O R S - - - - - */
public PortalJPA(){
}
public PortalJPA(ILatLngLocation location) {
try {
this.shop = INGRESS_FACTORY.getInventoryBuilder().build();
} catch (BuilderException e) {
e.printStackTrace();
}
this.links = new ArrayList<>();
this.resonators = new ArrayList<>();
this.location = location;
}
/* - - - - - G E T T E R S - A N D - S E T T E R S - - - - - */
@Override
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToOne( targetEntity = LatLngLocationJPA.class,
cascade = CascadeType.ALL)
@JoinColumn(name = "LOCATION_ID")
public ILatLngLocation getLocation() {
return location;
}
/* -- CRUD Sur la liste des liens -- */
@ManyToMany(targetEntity = LinkJPA.class,
cascade = CascadeType.ALL,
fetch = FetchType.LAZY)
@JoinTable
(
name="PORTAL_LINK",
joinColumns={ @JoinColumn(name="PORTAL_ID", referencedColumnName="ID") },
inverseJoinColumns={ @JoinColumn(name="LINK_ID", referencedColumnName="ID", unique=true) }
)
public List<ILink> getLinks() {
return links;
}
public ILink getLinkById(int id) throws SeekForIdException {
Optional<ILink> link = getLinks()
.stream()
.filter(p->p.getId() == id)
.findFirst();
if(!link.isPresent()) throw new SeekForIdException(id);
return link.get();
}
public void addLinks(List<ILink> links) {
this.links.addAll(links);
}
public void addLinks(ILink ... links) {
this.links.addAll(Arrays.asList(links));
}
public void addLink(ILink link) {
this.links.add(link);
}
public void removeAllLinks(){
this.links.clear();
}
public void removeLinkById(int id) throws SeekForIdException {
this.links.remove(getLinkById(id));
}
/* -- Fin CRUD -- */
/* -- CRUD Sur la liste des resonateurs */
@OneToMany(targetEntity = ResonatorJPA.class,
cascade = CascadeType.ALL)
@JoinTable
(
name="PORTAL_RESONATOR",
joinColumns={ @JoinColumn(name="PORTAL_ID", referencedColumnName="ID") },
inverseJoinColumns={ @JoinColumn(name="RESONATOR_ID", referencedColumnName="ID", unique=true) }
)
public List<IResonator> getResonators()
{
return resonators;
}
public void setResonators(List<IResonator> resonators) {
this.resonators = resonators;
}
public IResonator getResonatorById(int id) throws SeekForIdException {
Optional<IResonator> resonator = getResonators()
.stream()
.filter(p->p.getId() == id)
.findFirst();
if(! resonator.isPresent()) throw new SeekForIdException(id);
return resonator.get();
}
public void addResonators(List<IResonator> resonators) {
this.resonators.addAll(resonators);
}
public void addResonators(IResonator ... resonators){
this.resonators.addAll(Arrays.asList(resonators));
}
public void addResonator(IResonator resonator){
this.resonators.add(resonator);
}
public void deleteAllResonators(){this.resonators.clear();}
public void deleteResonatorById(int id) throws SeekForIdException {
this.resonators.remove(getResonatorById(id));
}
/* -- Fin CRUD -- */
@OneToOne( targetEntity = InventoryJPA.class,
cascade = CascadeType.ALL)
@JoinColumn(name = "SHOP_ID")
public IInventory getShop() {
return (IInventory) shop;
}
public void setShop(IInventory shop) {
this.shop = shop;
}
@ManyToOne( targetEntity = FactionJPA.class,
cascade = CascadeType.ALL)
@JoinColumn(name="FACTION_ID")
public IFaction getFaction() {
return (IFaction) faction;
}
public void setFaction(IFaction faction) {
this.faction = faction;
}
/* - - - - - T O - S T R I N G - - - - - */
//TODO Ajouter Les portails atteints.
@Override
public String toString() {
return
"id : " + getId() + "\n" +
"location : " + getLocation().getId() + "\n" +
"shop : " + getShop().getId() + "\n" +
"reached links : " + getIdsFromIdentifiableList(getLinks()) + "\n" +
"surrounded by : " + getIdsFromIdentifiableList(getResonators()) + "\n" +
"acquired by : " + getFaction() + "\n";
}
} |
Bon je sais qu'il y a pas mal d'erreur, dont les constructeurs et le toString qui devraient être côté Client, mais là n'est pas mon problème.
Avec ce code la méthode :
Code:
1 2 3 4 5
|
public List<IResonator> getResonators()
{
return resonators;
} |
n'est pas acceptée puisque le parent à pour signature List<IResonatorClient> getResonators().
J'ai donc essayé :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
public class PortalClient implements IPortalClient {
protected IInventoryClient shop;
protected ILatLngLocationClient location;
protected IFactionClient faction;
protected List<? extends ILinkClient> links;
protected List<? extends IResonatorClient> resonators;
@Override
public ILatLngLocationClient getLocation() {
return location;
}
@Override
public IInventoryClient getShop() {
return shop;
}
@Override
public IFactionClient getFaction() {
return faction;
}
@Override
public List<? extends ILinkClient> getLinks() {
return links;
}
@Override
public List<? extends IResonatorClient> getResonators() {
return resonators;
}
} |
Mais l'erreur est toujours présente et je ne comprends pas forcément pourquoi.
Si vous avez des idées je suis preneur !
Je vous remercie d'avance !
PS : Pour les attributs du client peut - on aussi utiliser la WildCard quand il ne s'agit pas d'une liste ?