Commit 645a13c0 by Tuukka Kivilahti

lisensecodes

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