Author: bleny Date: 2013-06-06 18:22:20 +0200 (Thu, 06 Jun 2013) New Revision: 240 Url: http://forge.codelutin.com/projects/franciaflex-magalie/repository/revisions... Log: remove location13 from fixtures (integrity violation) ; check origin and destination locations are accessible in storage transfer so fixes #2164 Added: trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/exception/InaccessibleLocationException.java Modified: trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/persistence/dao/LocationDao.java trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/persistence/dao/LocationJpaDao.java trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/service/MagalieBarcodeService.java trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/service/StorageTransferService.java trunk/magalie-services/src/main/resources/fixtures.yaml trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/StorageTransferAction.java trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/StorageTransferLocationAction.java Modified: trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/persistence/dao/LocationDao.java =================================================================== --- trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/persistence/dao/LocationDao.java 2013-06-05 12:29:41 UTC (rev 239) +++ trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/persistence/dao/LocationDao.java 2013-06-06 16:22:20 UTC (rev 240) @@ -37,5 +37,5 @@ List<Location> findAllWithoutReception(Warehouse warehouse); - Location findByBarCode(String barCode, Building building); + Location findByBarcode(String barcode, Building building); } Modified: trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/persistence/dao/LocationJpaDao.java =================================================================== --- trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/persistence/dao/LocationJpaDao.java 2013-06-05 12:29:41 UTC (rev 239) +++ trunk/magalie-persistence/src/main/java/com/franciaflex/magalie/persistence/dao/LocationJpaDao.java 2013-06-06 16:22:20 UTC (rev 240) @@ -83,11 +83,11 @@ } @Override - public Location findByBarCode(String barCode, Building building) { + public Location findByBarcode(String barcode, Building building) { TypedQuery<Location> query = createQuery("from Location l " + - "where CONCAT(l.warehouse.code, l.code) = :barCode " + + "where CONCAT(l.warehouse.code, l.code) = :barcode " + "and l.warehouse.building = :building"); - query.setParameter("barCode", barCode); + query.setParameter("barcode", barcode); query.setParameter("building", building); return findUniqueOrNull(query); } Added: trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/exception/InaccessibleLocationException.java =================================================================== --- trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/exception/InaccessibleLocationException.java (rev 0) +++ trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/exception/InaccessibleLocationException.java 2013-06-06 16:22:20 UTC (rev 240) @@ -0,0 +1,42 @@ +package com.franciaflex.magalie.services.exception; + +import com.franciaflex.magalie.persistence.Locations; +import com.franciaflex.magalie.persistence.entity.Location; +import com.franciaflex.magalie.persistence.entity.MagalieUser; + +public class InaccessibleLocationException extends MagalieException { + + protected Location location; + + protected MagalieUser magalieUser; + + public InaccessibleLocationException(String message, Location location, MagalieUser magalieUser) { + super(message); + this.location = location; + this.magalieUser = magalieUser; + } + + public static void checkLocationIsAccessible(Location location, MagalieUser magalieUser) throws InaccessibleLocationException { + + if (Locations.inaccessibleLocationPredicate(magalieUser).apply(location)) { + + InaccessibleLocationException newException = + new InaccessibleLocationException( + "user is not allowed to access given location", + location, magalieUser); + + throw newException; + + } + + } + + public Location getLocation() { + return location; + } + + public MagalieUser getMagalieUser() { + return magalieUser; + } + +} Modified: trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/service/MagalieBarcodeService.java =================================================================== --- trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/service/MagalieBarcodeService.java 2013-06-05 12:29:41 UTC (rev 239) +++ trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/service/MagalieBarcodeService.java 2013-06-06 16:22:20 UTC (rev 240) @@ -23,9 +23,13 @@ * #L% */ +import com.franciaflex.magalie.persistence.JpaMagaliePersistenceContext; import com.franciaflex.magalie.persistence.dao.ArticleDao; +import com.franciaflex.magalie.persistence.dao.LocationDao; import com.franciaflex.magalie.persistence.entity.Article; +import com.franciaflex.magalie.persistence.entity.Building; import com.franciaflex.magalie.persistence.entity.Company; +import com.franciaflex.magalie.persistence.entity.Location; import com.franciaflex.magalie.services.MagalieService; import com.franciaflex.magalie.services.MagalieServiceContext; import com.franciaflex.magalie.services.exception.InvalidMagalieBarcodeException; @@ -56,4 +60,23 @@ } + public Location getLocation(String locationBarcode, Building building) throws InvalidMagalieBarcodeException { + + JpaMagaliePersistenceContext persistenceContext = serviceContext.getPersistenceContext(); + + LocationDao locationDao = persistenceContext.getLocationDao(); + + Location location = locationDao.findByBarcode(locationBarcode, building); + + if (location == null) { + + throw new InvalidMagalieBarcodeException( + "no location with code " + locationBarcode); + + } + + return location; + + } + } Modified: trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/service/StorageTransferService.java =================================================================== --- trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/service/StorageTransferService.java 2013-06-05 12:29:41 UTC (rev 239) +++ trunk/magalie-services/src/main/java/com/franciaflex/magalie/services/service/StorageTransferService.java 2013-06-06 16:22:20 UTC (rev 240) @@ -25,7 +25,6 @@ import com.franciaflex.magalie.persistence.JpaMagaliePersistenceContext; -import com.franciaflex.magalie.persistence.dao.LocationDao; import com.franciaflex.magalie.persistence.dao.LocationJpaDao; import com.franciaflex.magalie.persistence.dao.StorageMovementDao; import com.franciaflex.magalie.persistence.dao.StorageMovementJpaDao; @@ -38,6 +37,8 @@ import com.franciaflex.magalie.persistence.entity.StoredArticle; import com.franciaflex.magalie.services.MagalieService; import com.franciaflex.magalie.services.MagalieServiceContext; +import com.franciaflex.magalie.services.exception.InaccessibleLocationException; +import com.franciaflex.magalie.services.exception.InvalidMagalieBarcodeException; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import org.apache.commons.logging.Log; @@ -58,15 +59,18 @@ this.serviceContext = serviceContext; } - public Location getLocationByBarCode(String barCode, Building building) { + public Location getLocation(Building building, MagalieUser magalieUser, String barcode) + throws InvalidMagalieBarcodeException, InaccessibleLocationException { - JpaMagaliePersistenceContext persistenceContext = serviceContext.getPersistenceContext(); + MagalieBarcodeService magalieBarcodeService = + serviceContext.newService(MagalieBarcodeService.class); - LocationDao locationDao = persistenceContext.getLocationDao(); + Location location = magalieBarcodeService.getLocation(barcode, building); - Location location = locationDao.findByBarCode(barCode, building); + InaccessibleLocationException.checkLocationIsAccessible(location, magalieUser); return location; + } public Location getLocationById(String id) { Modified: trunk/magalie-services/src/main/resources/fixtures.yaml =================================================================== --- trunk/magalie-services/src/main/resources/fixtures.yaml 2013-06-05 12:29:41 UTC (rev 239) +++ trunk/magalie-services/src/main/resources/fixtures.yaml 2013-06-06 16:22:20 UTC (rev 240) @@ -216,14 +216,6 @@ requiredAccreditationLevel: 0 requiredCraneMan : false -location13: - &location13 !location - id: location13 - warehouse: *U01 - code: REC - requiredAccreditationLevel: 0 - requiredCraneMan : false - location14: &location14 !location id: location14 @@ -302,7 +294,6 @@ - *location10 - *location11 - *location12 - - *location13 - *location14 - *locationRecU01 - *locationRecU02 @@ -476,7 +467,7 @@ &storedArticle5 !stored-article id: storedArticle5 article: *article1 - location: *location13 + location: *locationRecU01 quantity: 30 storedArticle6: Modified: trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/StorageTransferAction.java =================================================================== --- trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/StorageTransferAction.java 2013-06-05 12:29:41 UTC (rev 239) +++ trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/StorageTransferAction.java 2013-06-06 16:22:20 UTC (rev 240) @@ -25,7 +25,10 @@ import com.franciaflex.magalie.persistence.entity.Building; import com.franciaflex.magalie.persistence.entity.Location; +import com.franciaflex.magalie.persistence.entity.MagalieUser; import com.franciaflex.magalie.persistence.entity.StoredArticle; +import com.franciaflex.magalie.services.exception.InaccessibleLocationException; +import com.franciaflex.magalie.services.exception.InvalidMagalieBarcodeException; import com.franciaflex.magalie.services.service.StorageTransferService; import com.franciaflex.magalie.web.MagalieActionSupport; import com.franciaflex.magalie.web.MagalieSession; @@ -165,11 +168,26 @@ Building building = session.getBuilding(); - Location destination = service.getLocationByBarCode(destinationBarCode, building); + MagalieUser magalieUser = session.getMagalieUser(); - if (destination == null) { + Location destination; + + try { + + destination = service.getLocation(building, magalieUser, destinationBarCode); + + } catch (InvalidMagalieBarcodeException e) { + addFieldError("destinationBarCode", "Le code barre n'est pas un code valide"); + return INPUT; + + } catch (InaccessibleLocationException e) { + + addFieldError("destinationBarCode", "Vous n'êtes pas autorisé à accéder à l'emplacement"); + + return INPUT; + } if (destination.equals(storedArticle.getLocation())) { @@ -177,9 +195,10 @@ return INPUT; } - service.confirmStorageTransfer(session.getMagalieUser(), storedArticle, quantity, destination); + service.confirmStorageTransfer(magalieUser, storedArticle, quantity, destination); return SUCCESS; + } } Modified: trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/StorageTransferLocationAction.java =================================================================== --- trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/StorageTransferLocationAction.java 2013-06-05 12:29:41 UTC (rev 239) +++ trunk/magalie-web/src/main/java/com/franciaflex/magalie/web/action/StorageTransferLocationAction.java 2013-06-06 16:22:20 UTC (rev 240) @@ -25,7 +25,10 @@ import com.franciaflex.magalie.persistence.entity.Building; import com.franciaflex.magalie.persistence.entity.Location; +import com.franciaflex.magalie.persistence.entity.MagalieUser; import com.franciaflex.magalie.persistence.entity.StoredArticle; +import com.franciaflex.magalie.services.exception.InaccessibleLocationException; +import com.franciaflex.magalie.services.exception.InvalidMagalieBarcodeException; import com.franciaflex.magalie.services.service.StorageTransferService; import com.franciaflex.magalie.web.MagalieActionSupport; import com.franciaflex.magalie.web.MagalieSession; @@ -76,28 +79,41 @@ Building building = session.getBuilding(); - origin = service.getLocationByBarCode(originBarCode, building); + MagalieUser magalieUser = session.getMagalieUser(); - if (origin == null) { + try { + + origin = service.getLocation(building, magalieUser, originBarCode); + + } catch (InvalidMagalieBarcodeException e) { + addFieldError("originBarCode", "Le code barre n'est pas un code valide"); + return INPUT; + + } catch (InaccessibleLocationException e) { + + addFieldError("originBarCode", "Vous n'êtes pas autorisé à accéder à l'emplacement"); + + return INPUT; + } List<StoredArticle> storedArticles = service.getStoredArticlesInLocation(origin); if (storedArticles.isEmpty()) { - addFieldError("originBarCode", "Aucun articles dans cet emplacement"); + + addActionMessage("Aucun article dans cet emplacement"); + return INPUT; + } return SUCCESS; + } public String getOriginId() { - String originId = ""; - if (origin != null) { - originId = origin.getId(); - } - return originId; + return origin.getId(); } }