Commit dc5dd3ac by Tuomas Riihimäki

Merge placecode generation to fetching class

1 parent 885340b6
......@@ -111,7 +111,8 @@ public interface PlaceBeanLocal {
/**
* Get all products used in map;
* @param map
*
* @param map
*
* @return
*/
......@@ -133,4 +134,6 @@ public interface PlaceBeanLocal {
EventMap copyMap(EventMap sourceMap, EventMap map);
Place findByCode(String code, EventMap map);
}
......@@ -802,4 +802,9 @@ public class PlaceBean implements PlaceBeanLocal {
return dst;
}
@Override
public Place findByCode(String code, EventMap map) {
return placeFacade.findByCode(code, map);
}
}
......@@ -309,4 +309,12 @@ public class PlaceFacade extends IntegerPkGenericFacade<Place> {
return getEm().createQuery(cq).getResultList();
}
public Place findByCode(String code, EventMap map) {
CriteriaBuilder cb = getEm().getCriteriaBuilder();
CriteriaQuery<Place> cq = cb.createQuery(Place.class);
Root<Place> root = cq.from(Place.class);
cq.where(cb.equal(root.get(Place_.map), map), cb.equal(root.get(Place_.code), code));
return super.getSingleNullableResult(getEm().createQuery(cq));
}
}
......@@ -226,7 +226,13 @@ public class PlacemapRestViewV1 {
ArrayList<PlaceCodePojo> ret = new ArrayList<PlaceCodePojo>();
for (Place p : map.getPlaces()) {
if (p.getCode() == null || p.getCode().isEmpty()) {
return Response.status(Response.Status.NOT_FOUND).entity("Error fetching code for place " + p.getName()).build();
String newcode = null;
do {
newcode = PasswordFunctions.generateRandomString(12, BarcodeBeanLocal.TEXTCODE_CHARACTER_MAP);
} while (placebean.findByCode(newcode, map) != null);
p.setCode(newcode);
p = placebean.mergeChanges(p);
logger.info("Updating place {} with code {} in map {}", p.getName(), p.getCode(), map);
}
ret.add(new PlaceCodePojo(p));
}
......@@ -235,45 +241,4 @@ public class PlacemapRestViewV1 {
}
@GET
@Path("/{id}/gencodes")
public Response getGencodes(@PathParam("id") Integer mapId) {
if (!permbean.hasPermission(MapPermission.MANAGE_MAPS)) {
return Response
.status(Response.Status.FORBIDDEN)
.entity("No enough permissions!")
.build();
}
EventMap map = placebean.findMap(mapId);
LanEvent event = eventbean.getCurrentEvent();
if (!event.equals(map.getEvent())) {
return Response
.status(Response.Status.FORBIDDEN)
.entity("Wrong event!")
.build();
}
Set<String> oldcodes = new HashSet<>();
for (Place p : map.getPlaces()) {
if (p.getCode() != null && !p.getCode().isEmpty()) {
oldcodes.add(p.getCode());
}
}
for (Place p : map.getPlaces()) {
String code = null;
do {
code = PasswordFunctions.generateRandomString(12, BarcodeBeanLocal.TEXTCODE_CHARACTER_MAP);
} while (oldcodes.contains(code));
if (p.getCode() == null || p.getCode().isEmpty()) {
p.setCode(code);
p = placebean.mergeChanges(p);
logger.info("Updating place {} with code {}", p.getName(), p.getCode());
}
}
return Response.ok().entity("Ok").build();
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!