Commit 645a13c0 by Tuukka Kivilahti

lisensecodes

1 parent 5d83d6a4
...@@ -12,27 +12,33 @@ import java.util.List; ...@@ -12,27 +12,33 @@ import java.util.List;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.ejb.Stateless; import javax.ejb.Stateless;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.facade.LicenseCodeFacade; import fi.codecrew.moya.facade.LicenseCodeFacade;
import fi.codecrew.moya.facade.LicenseTargetFacade; import fi.codecrew.moya.facade.LicenseTargetFacade;
import fi.codecrew.moya.model.EventMap; import fi.codecrew.moya.facade.UserFacade;
import fi.codecrew.moya.model.EventUser; import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LicenseTarget;
import fi.codecrew.moya.model.LicenseCode;
import fi.codecrew.moya.model.GroupMembership; import fi.codecrew.moya.model.GroupMembership;
import fi.codecrew.moya.model.LanEvent; import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.Place; import fi.codecrew.moya.model.LicenseCode;
import fi.codecrew.moya.model.LicenseTarget;
import fi.codecrew.moya.model.User;
/** /**
* Session Bean implementation class GameBean * Session Bean implementation class GameBean
*/ */
@Stateless @Stateless
public class LicenseBean implements LicenseBeanLocal { public class LicenseBean implements LicenseBeanLocal {
@EJB
LicenseCodeFacade licenseCodeFacade;
@EJB @EJB
LicenseCodeFacade gameCodeFacade; UserFacade userFacade;
@EJB @EJB
LicenseTargetFacade gameFacade; LicenseTargetFacade licenseTargetFacade;
@EJB @EJB
...@@ -41,30 +47,22 @@ public class LicenseBean implements LicenseBeanLocal { ...@@ -41,30 +47,22 @@ public class LicenseBean implements LicenseBeanLocal {
@EJB @EJB
PlaceBeanLocal placeBean; PlaceBeanLocal placeBean;
private static final Logger logger = LoggerFactory.getLogger(LicenseBean.class);
public List<LicenseCode> findUserCodes(EventUser user) {
ArrayList<LicenseCode> returnCodes = new ArrayList<LicenseCode>();
for(LicenseCode userGameCode : user.getUser().getGameCodes()) {
returnCodes.add(userGameCode);
}
return returnCodes;
}
/** /**
* Check, and if needed generate code for gamecode. * Check, and if needed generate code for gamecode.
* *
* @param code * @param code
*/ */
private boolean generateCode(LicenseCode code) { private LicenseCode generateCode(LicenseCode code) throws GenerationException {
if (code.getCode() == null || code.getCode().trim().equals("")) { if (code.getCode() == null || code.getCode().trim().equals("")) {
if (code.getGame().getCodeUrl() == null || code.getGame().getCodeUrl().trim().equals("")) if (code.getLicenseTarget().getCodeUrl() == null || code.getLicenseTarget().getCodeUrl().trim().equals("")) {
return false; throw new GenerationException("Code generate failed");
}
try { try {
URL url = new URL(code.getGame().getCodeUrl()); URL url = new URL(code.getLicenseTarget().getCodeUrl());
URLConnection uc; URLConnection uc;
...@@ -82,55 +80,50 @@ public class LicenseBean implements LicenseBeanLocal { ...@@ -82,55 +80,50 @@ public class LicenseBean implements LicenseBeanLocal {
} }
if(codeString.trim().equals("0") || codeString.trim().equals("")) { if(codeString.trim().equals("0") || codeString.trim().equals("")) {
return false; throw new GenerationException("Code generate failed");
} }
code.setCode(codeString); code.setCode(codeString);
code = gameCodeFacade.merge(code); code = licenseCodeFacade.merge(code);
return true; return code;
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block logger.warn("Code generate failed", e);
e.printStackTrace(); throw new GenerationException("Code generate failed");
} }
} }
return false; throw new RuntimeException("LOL, what?");
} }
public boolean accessCode(LicenseCode code, EventUser user) { public LicenseCode accessCode(LicenseCode code) throws GenerationException {
if(code.getUser() != null)
return false;
if(!generateCode(code)) { code = generateCode(code);
return false;
}
if (!code.isAccessed()) { if (!code.isAccessed()) {
code.setAccessed(Calendar.getInstance()); code.setAccessed(Calendar.getInstance());
code.setUser(user.getUser());
code = gameCodeFacade.merge(code); code = licenseCodeFacade.merge(code);
user.getUser().getGameCodes().add(code);
} }
return true; return code;
} }
public List<LicenseTarget> findAll(LanEvent event) { public List<LicenseTarget> findAll(LanEvent event) {
return event.getGames(); return event.getGames();
} }
public void saveOrCreateLicense(LicenseTarget game) { public void saveOrCreateLicense(LicenseTarget target) {
if (game.getId() == null) { if (target.getId() == null) {
game.setEvent(eventBean.getCurrentEvent()); target.setEvent(eventBean.getCurrentEvent());
eventBean.getCurrentEvent().getGames().add(game); eventBean.getCurrentEvent().getGames().add(target);
gameFacade.create(game); licenseTargetFacade.create(target);
game = gameFacade.merge(game); target = licenseTargetFacade.merge(target);
} }
else { else {
game = gameFacade.merge(game); target = licenseTargetFacade.merge(target);
} }
} }
...@@ -146,14 +139,16 @@ public class LicenseBean implements LicenseBeanLocal { ...@@ -146,14 +139,16 @@ public class LicenseBean implements LicenseBeanLocal {
ArrayList<LicenseTarget> returnLicenses = new ArrayList<LicenseTarget>(); ArrayList<LicenseTarget> returnLicenses = new ArrayList<LicenseTarget>();
if(user.getCurrentPlaces() != null) { if(user.getGroupMemberships() != null) {
for(GroupMembership memberShip : user.getGroupMemberships()) { for(GroupMembership memberShip : user.getGroupMemberships()) {
if(memberShip.getPlaceReservation() == null) if(memberShip.getPlaceReservation() == null)
continue; continue;
for(LicenseTarget license : memberShip.getPlaceReservation().getProduct().getLicenseTargets()) { for(LicenseTarget license : memberShip.getPlaceReservation().getProduct().getLicenseTargets()) {
if(license.isActive()) if(license.isActive()) {
returnLicenses.add(license); returnLicenses.add(license);
}
} }
} }
} }
...@@ -163,15 +158,46 @@ public class LicenseBean implements LicenseBeanLocal { ...@@ -163,15 +158,46 @@ public class LicenseBean implements LicenseBeanLocal {
@Override @Override
public List<LicenseTarget> findUnopenedUserGames(EventUser user) { public List<LicenseTarget> findUnopenedUserGames(EventUser user) {
List<LicenseTarget> returnLicenses = findUserGames(user); List<LicenseTarget> returnLicenses = findUserGames(user);
for(LicenseCode code : findUserCodes(user)) { for(LicenseCode code : user.getUser().getLicenseCodes()) {
returnLicenses.remove(code); returnLicenses.remove(code.getLicenseTarget());
} }
return returnLicenses; return returnLicenses;
} }
@Override
public LicenseCode createAndAccessCode(LicenseTarget target, User user) throws GenerationException {
user = userFacade.reload(user);
LicenseCode code = new LicenseCode(target);
user.getLicenseCodes().add(code);
code.setUser(user);
licenseCodeFacade.create(code);
code = accessCode(code);
return code;
}
} }
...@@ -88,9 +88,9 @@ public class UserFacade extends IntegerPkGenericFacade<User> { ...@@ -88,9 +88,9 @@ public class UserFacade extends IntegerPkGenericFacade<User> {
// } // }
@Override @Override
public void create(User user) { public User create(User user) {
user.setLogin(user.getLogin().toLowerCase().trim()); user.setLogin(user.getLogin().toLowerCase().trim());
super.create(user); return super.create(user);
} }
@Override @Override
......
...@@ -2,22 +2,39 @@ package fi.codecrew.moya.beans; ...@@ -2,22 +2,39 @@ package fi.codecrew.moya.beans;
import java.util.List; import java.util.List;
import javax.ejb.ApplicationException;
import javax.ejb.Local; import javax.ejb.Local;
import fi.codecrew.moya.model.EventUser; import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LicenseTarget;
import fi.codecrew.moya.model.LicenseCode;
import fi.codecrew.moya.model.LanEvent; import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.Place; import fi.codecrew.moya.model.LicenseCode;
import fi.codecrew.moya.model.LicenseTarget;
import fi.codecrew.moya.model.User;
@Local @Local
public interface LicenseBeanLocal { public interface LicenseBeanLocal {
public boolean accessCode(LicenseCode code, EventUser user); public LicenseCode accessCode(LicenseCode code) throws GenerationException;
public List<LicenseTarget> findAll(LanEvent event); public List<LicenseTarget> findAll(LanEvent event);
public void saveOrCreateLicense(LicenseTarget game); public void saveOrCreateLicense(LicenseTarget game);
public List<LicenseCode> findUserCodes(EventUser user);
public List<LicenseTarget> findUserGames(EventUser user); public List<LicenseTarget> findUserGames(EventUser user);
public List<LicenseTarget> findUnopenedUserGames(EventUser user); public List<LicenseTarget> findUnopenedUserGames(EventUser user);
public LicenseCode createAndAccessCode(LicenseTarget target, User user) throws GenerationException;
@ApplicationException(rollback=true)
public static class GenerationException extends Exception {
public GenerationException() {
super();
}
public GenerationException(String message) {
super(message);
}
/**
*
*/
private static final long serialVersionUID = 1L;
}
} }
\ No newline at end of file
...@@ -36,16 +36,12 @@ public class LicenseCode extends GenericEntity { ...@@ -36,16 +36,12 @@ public class LicenseCode extends GenericEntity {
@JoinColumn(name = "game_id", referencedColumnName = "id") @JoinColumn(name = "game_id", referencedColumnName = "id")
@ManyToOne @ManyToOne
private LicenseTarget game; private LicenseTarget licenseTarget;
@JoinColumn(name = "user_id", referencedColumnName = "id") @JoinColumn(name = "user_id", referencedColumnName = "id")
@ManyToOne @ManyToOne
private User user; private User user;
@JoinColumn(name = "place_id", referencedColumnName = "id")
@ManyToOne
private Place place;
public LicenseCode() { public LicenseCode() {
...@@ -53,10 +49,9 @@ public class LicenseCode extends GenericEntity { ...@@ -53,10 +49,9 @@ public class LicenseCode extends GenericEntity {
} }
public LicenseCode(Place place, LicenseTarget game) { public LicenseCode(LicenseTarget target) {
this(); this();
this.place = place; this.licenseTarget = target;
this.game = game;
} }
...@@ -80,46 +75,25 @@ public class LicenseCode extends GenericEntity { ...@@ -80,46 +75,25 @@ public class LicenseCode extends GenericEntity {
return code; return code;
} }
public void setCode(String code) { public void setCode(String code) {
this.code = code; this.code = code;
} }
public LicenseTarget getGame() {
return game;
}
public void setGame(LicenseTarget game) {
this.game = game;
}
public User getUser() { public User getUser() {
return user; return user;
} }
public void setUser(User user) { public void setUser(User user) {
this.user = user; this.user = user;
} }
public LicenseTarget getLicenseTarget() {
return licenseTarget;
public Place getPlace() {
return place;
} }
public void setLicenseTarget(LicenseTarget licenseTarget) {
public void setPlace(Place place) { this.licenseTarget = licenseTarget;
this.place = place;
} }
} }
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
*/ */
package fi.codecrew.moya.model; package fi.codecrew.moya.model;
import java.util.Calendar;
import java.util.List; import java.util.List;
import javax.persistence.Column; import javax.persistence.Column;
...@@ -15,8 +14,6 @@ import javax.persistence.ManyToOne; ...@@ -15,8 +14,6 @@ import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.OrderBy; import javax.persistence.OrderBy;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.eclipse.persistence.annotations.OptimisticLocking; import org.eclipse.persistence.annotations.OptimisticLocking;
import org.eclipse.persistence.annotations.OptimisticLockingType; import org.eclipse.persistence.annotations.OptimisticLockingType;
...@@ -52,7 +49,7 @@ public class LicenseTarget extends GenericEntity { ...@@ -52,7 +49,7 @@ public class LicenseTarget extends GenericEntity {
@ManyToOne @ManyToOne
private LanEvent event; private LanEvent event;
@OneToMany(mappedBy = "game", fetch = FetchType.LAZY) @OneToMany(mappedBy = "licenseTarget", fetch = FetchType.LAZY)
@OrderBy() @OrderBy()
private List<LicenseCode> licenseCodes; private List<LicenseCode> licenseCodes;
......
...@@ -113,7 +113,7 @@ public class User extends GenericEntity implements IUser { ...@@ -113,7 +113,7 @@ public class User extends GenericEntity implements IUser {
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY) @OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
@OrderBy() @OrderBy()
private List<LicenseCode> gameCodes; private List<LicenseCode> licenseCodes;
@Transient @Transient
private static final Logger logger = LoggerFactory.getLogger(User.class); private static final Logger logger = LoggerFactory.getLogger(User.class);
...@@ -361,15 +361,15 @@ public class User extends GenericEntity implements IUser { ...@@ -361,15 +361,15 @@ public class User extends GenericEntity implements IUser {
} }
public List<LicenseCode> getGameCodes() { public List<LicenseCode> getLicenseCodes() {
if(gameCodes == null) if(licenseCodes == null)
gameCodes = new ArrayList<LicenseCode>(); licenseCodes = new ArrayList<LicenseCode>();
return gameCodes; return licenseCodes;
} }
public void setGameCodes(List<LicenseCode> gameCodes) { public void setLicenseCodes(List<LicenseCode> codes) {
this.gameCodes = gameCodes; this.licenseCodes = codes;
} }
} }
...@@ -38,8 +38,10 @@ public abstract class GenericFacade<C extends ModelInterface> { ...@@ -38,8 +38,10 @@ public abstract class GenericFacade<C extends ModelInterface> {
protected abstract EntityManager getEm(); protected abstract EntityManager getEm();
public void create(C entity) { public C create(C entity) {
getEm().persist(entity); getEm().persist(entity);
return entity;
} }
public void remove(C entity) { public void remove(C entity) {
......
...@@ -86,12 +86,14 @@ error.error = You have encountered an error. ...@@ -86,12 +86,14 @@ error.error = You have encountered an error.
eventorg.create = Create eventorg.create = Create
game.active = Aktiivinen game.active = Aktiivinen
game.codecount = Avattuja game.codecount = Avattuja
game.create = Create game.codes.available = Lisenssikoodit
game.edit = Edit game.codes.opened = Avatut lisenssikoodit
game.out = Gamecodes are out, pleace contact administration game.create = Create
game.product = Tuote game.edit = Edit
game.out = Gamecodes are out, pleace contact administration
game.product = Tuote
generic.sure.header = Confirmation generic.sure.header = Confirmation
generic.sure.message = Are you sure? generic.sure.message = Are you sure?
......
...@@ -277,19 +277,21 @@ foodwavetemplate.selectproducts = Products ...@@ -277,19 +277,21 @@ foodwavetemplate.selectproducts = Products
foodwavetemplate.startTime = Foodwave time foodwavetemplate.startTime = Foodwave time
foodwavetemplate.waveName = Wave name foodwavetemplate.waveName = Wave name
game.active = Active game.active = Active
game.code = Code game.code = Code
game.codecount = Opened game.codecount = Opened
game.create = Create game.codes.available = Licensecodes
game.description = Description game.codes.opened = Opened licensecodes
game.edit = Edit game.create = Create
game.gamepoints = Game points game.description = Description
game.name = Name game.edit = Edit
game.noGameCodes = You have no gamecodes game.gamepoints = Game points
game.open = Open code game.name = Name
game.out = Please contact out customer service game.noGameCodes = You have no opened gamecodes
game.product = Product game.open = Open code
game.service = Game service game.out = Please contact out customer service
game.product = Product
game.service = Game service
gamepoints = Gamepoints gamepoints = Gamepoints
......
...@@ -277,19 +277,21 @@ foodwavetemplate.selectproducts = Tuotteet ...@@ -277,19 +277,21 @@ foodwavetemplate.selectproducts = Tuotteet
foodwavetemplate.startTime = Tilausaika foodwavetemplate.startTime = Tilausaika
foodwavetemplate.waveName = Tilauksen nimi foodwavetemplate.waveName = Tilauksen nimi
game.active = Aktiivinen game.active = Aktiivinen
game.code = Koodi game.code = Koodi
game.codecount = Avattuja game.codecount = Avattuja
game.create = Luo game.codes.available = Lisenssikoodit
game.description = Kuvaus game.codes.opened = Avatut lisenssikoodit
game.edit = Muokkaa game.create = Luo
game.gamepoints = Insomnia Game pisteet: game.description = Kuvaus
game.name = Nimi game.edit = Muokkaa
game.noGameCodes = Sinulla ei ole pelikoodeja game.gamepoints = Insomnia Game pisteet:
game.open = Ota koodi k\u00E4ytt\u00F6\u00F6n game.name = Nimi
game.out = Ei voitu avata pelikoodia, ota yhteytt\u00E4 asiakaspalveluun. game.noGameCodes = Sinulla ei ole avattuja pelikoodeja.
game.product = Tuote game.open = Ota koodi k\u00E4ytt\u00F6\u00F6n
game.service = Pelipalvelu game.out = Ei voitu avata pelikoodia, ota yhteytt\u00E4 asiakaspalveluun.
game.product = Tuote
game.service = Pelipalvelu
gamepoints = Pelipisteit\u00E4 gamepoints = Pelipisteit\u00E4
......
...@@ -5,18 +5,18 @@ import java.util.List; ...@@ -5,18 +5,18 @@ import java.util.List;
import javax.ejb.EJB; import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped; import javax.enterprise.context.ConversationScoped;
import javax.faces.model.ListDataModel; import javax.faces.model.ListDataModel;
import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import fi.codecrew.moya.beans.EventBeanLocal; import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.LicenseBeanLocal; import fi.codecrew.moya.beans.LicenseBeanLocal;
import fi.codecrew.moya.beans.LicenseBeanLocal.GenerationException;
import fi.codecrew.moya.beans.PermissionBeanLocal;
import fi.codecrew.moya.beans.ProductBeanLocal; import fi.codecrew.moya.beans.ProductBeanLocal;
import fi.codecrew.moya.enums.apps.LicensePermission; import fi.codecrew.moya.enums.apps.LicensePermission;
import fi.codecrew.moya.model.EventUser; import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LicenseCode; import fi.codecrew.moya.model.LicenseCode;
import fi.codecrew.moya.model.LicenseTarget; import fi.codecrew.moya.model.LicenseTarget;
import fi.codecrew.moya.model.Product; import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.web.annotations.SelectedUser;
import fi.codecrew.moya.web.cdiview.GenericCDIView; import fi.codecrew.moya.web.cdiview.GenericCDIView;
@Named @Named
...@@ -25,18 +25,19 @@ public class LicenseView extends GenericCDIView { ...@@ -25,18 +25,19 @@ public class LicenseView extends GenericCDIView {
private static final long serialVersionUID = -8346420143750551402L; private static final long serialVersionUID = -8346420143750551402L;
@Inject private EventUser currentUser;
@SelectedUser
private EventUser user; @EJB
private PermissionBeanLocal permissionBean;
@EJB @EJB
EventBeanLocal eventBean; private EventBeanLocal eventBean;
@EJB @EJB
LicenseBeanLocal licenseBean; private LicenseBeanLocal licenseBean;
@EJB @EJB
ProductBeanLocal productBean; private ProductBeanLocal productBean;
private ListDataModel<LicenseTarget> licenses; private ListDataModel<LicenseTarget> licenses;
private ListDataModel<LicenseCode> licenseCodes; private ListDataModel<LicenseCode> licenseCodes;
...@@ -57,8 +58,10 @@ public class LicenseView extends GenericCDIView { ...@@ -57,8 +58,10 @@ public class LicenseView extends GenericCDIView {
public void initUserView() { public void initUserView() {
if (super.requirePermissions(LicensePermission.VIEW_OWN_CODES)) { if (super.requirePermissions(LicensePermission.VIEW_OWN_CODES)) {
if (licenses == null) { if (licenseCodes == null) {
this.licenseCodes = new ListDataModel<LicenseCode>(licenseBean.findUserCodes(user)); currentUser = permissionBean.getCurrentUser();
this.licenseCodes = new ListDataModel<LicenseCode>(currentUser.getUser().getLicenseCodes());
this.licenses = new ListDataModel<LicenseTarget>(licenseBean.findUnopenedUserGames(currentUser));
this.beginConversation(); this.beginConversation();
} }
} }
...@@ -66,6 +69,7 @@ public class LicenseView extends GenericCDIView { ...@@ -66,6 +69,7 @@ public class LicenseView extends GenericCDIView {
public String saveCurrentLicense() { public String saveCurrentLicense() {
licenseBean.saveOrCreateLicense(currentLicense); licenseBean.saveOrCreateLicense(currentLicense);
this.licenses = new ListDataModel<LicenseTarget>(licenseBean.findAll(eventBean.getCurrentEvent()));
return null; return null;
} }
...@@ -75,20 +79,28 @@ public class LicenseView extends GenericCDIView { ...@@ -75,20 +79,28 @@ public class LicenseView extends GenericCDIView {
} }
public String openSelectedCode() { public String openSelectedCode() {
/*if (gameCodesDataModel != null && gameCodesDataModel.isRowAvailable()) {
LicenseCode code = gameCodesDataModel.getRowData(); if(this.licenses != null && this.licenses.isRowAvailable()) {
LicenseTarget license = this.licenses.getRowData();
if (!licenseBean.accessCode(code, user)) {
this.addFaceMessage("game.out"); try {
LicenseCode code = licenseBean.createAndAccessCode(license, currentUser.getUser());
} catch(GenerationException x) {
this.addFaceMessage("game.out");
} }
licenseCodes = null;
initUserView();
} }
*/ return null;
return "todo";
} }
public boolean isNoGameCodes() { public boolean isNoLicenseCodes() {
//return (getGameCodes().getRowCount() <= 0); return (getLicenseCodes().getRowCount() <= 0);
return false; }
public boolean isNoLicenses() {
return (getLicenses().getRowCount() <= 0);
} }
public ListDataModel<LicenseCode> getLicenseCodes() { public ListDataModel<LicenseCode> getLicenseCodes() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!