package dist.service; import dist.models.*; import models.OrderModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import service.PoseidonPropertyService; public class DistSyncer { private final static Logger logger = LoggerFactory.getLogger(DistSyncer.class); public static final String DIST_SERVER = "dist"; public void syncOrder(OrderModel order) { String locationName = PoseidonPropertyService.getProperty("dist.location.nameprefix") + String.valueOf(order.id); logger.debug("start syncOrder({},{})", order.id,locationName); saveLocation(locationName); saveProduct(locationName, order.id); saveProcessing(locationName); saveLocationHosts(locationName); saveDistProductLocation(locationName); } public void deleteOrder(OrderModel order) { logger.debug("deleteOrder: id {}, metref {}, status {}", order.id, order.met_ref,order.orderStatus); String productLocationName = PoseidonPropertyService.getProperty("dist.location.nameprefix") + String.valueOf(order.id); logger.debug("Sletter produkt {} fra dist db", productLocationName); deleteLocation(productLocationName); deleteLocationHosts(productLocationName); deleteProductLocation(productLocationName); deleteProduct(productLocationName); deleteProcessing(productLocationName); } private void deleteProduct(String productName) { DistProduct distProduct = DistProduct.find.byId(productName); if (distProduct != null) { logger.info("Sletter DistProduct {}",distProduct); distProduct.delete(DIST_SERVER); } else { logger.debug("Kan ikke slette produkt '{}'. Ikke funnet", productName); } } private void deleteProductLocation(String productName) { DistProductLocationsPK pk = new DistProductLocationsPK(); pk.product = productName; pk.location = productName; DistProductLocation pl = DistProductLocation.find.byId(pk); if (pl != null) { logger.info("Sletter DistProductLocation {}",pl); pl.delete(DIST_SERVER); } else { logger.debug("Kan ikke slette ProductLocation '{}','{}'. Ikke funnet", pk.product, pk.location); } } private void deleteLocationHosts(String locationName) { DistLocationHostPK hostPk = new DistLocationHostPK(); hostPk.host = PoseidonPropertyService.getProperty("dist.location.host1"); DistLocationHost host; hostPk.location = locationName; host = DistLocationHost.find.byId(hostPk); if (host != null) { logger.info("Sletter DistLocationHost 1 '{}','{}'", host.pk.location, host.pk.host); host.delete(DIST_SERVER); } else { logger.debug("Kan ikke slette LocationHost '{}','{}'. Ikke funnet", hostPk.location, hostPk.host); } hostPk.host = PoseidonPropertyService.getProperty("dist.location.host2"); host = DistLocationHost.find.byId(hostPk); if (host != null) { logger.info("Sletter DistLocationHost 2 '{}','{}'", host.pk.location, host.pk.host); host.delete(DIST_SERVER); } else { logger.debug("Kan ikke slette LocationHost '{}','{}'. Ikke funnet", hostPk.location, hostPk.host); } } private void deleteLocation(String locationName) { DistLocation location = DistLocation.find.byId(locationName); if (location != null) { logger.info("Slettet DistLocation {} ", location); location.delete(DIST_SERVER); } else { logger.debug("Kan ikke slette location '{}'. Ikke funnet", locationName); } } private void deleteProcessing(String name) { DistProcessing proc = DistProcessing.find.byId(name); if (proc != null) { logger.info("Slettet DistProcessing '{}'", name); proc.delete(DIST_SERVER); } else { logger.debug("Kan ikke slette processing '{}'. Ikke funnet", name); } } private void saveLocation(String locationName) { logger.debug("start saveLocation({})", locationName); DistLocation location = DistLocation.find.byId(locationName); if (location == null) { location = new DistLocation(); location.name = locationName; location.remote_info = PoseidonPropertyService.getProperty("dist.location.remote_info") + "/" + locationName; location.ipc_method = PoseidonPropertyService.getProperty("dist.location.ipc_method"); location.method_options = PoseidonPropertyService.getProperty("dist.location.method_options"); location.type = PoseidonPropertyService.getProperty("dist.location.type"); logger.info("saving location {}", location.name); location.insert(DIST_SERVER); } else { location.remote_info = PoseidonPropertyService.getProperty("dist.location.remote_info") + "/" + locationName; location.ipc_method = PoseidonPropertyService.getProperty("dist.location.ipc_method"); location.method_options = PoseidonPropertyService.getProperty("dist.location.method_options"); location.type = PoseidonPropertyService.getProperty("dist.location.type"); logger.debug("Location '{}' finnes allerede. Oppdaterer", location.name); location.update(DIST_SERVER); } } private void saveLocationHosts(String locationName) { DistLocationHostPK hostPk = new DistLocationHostPK(); hostPk.host = PoseidonPropertyService.getProperty("dist.location.host1"); saveDistLocationHost(locationName, hostPk); hostPk.host = PoseidonPropertyService.getProperty("dist.location.host2"); saveDistLocationHost(locationName, hostPk); } private void saveDistLocationHost(String locationName, DistLocationHostPK hostPk) { DistLocationHost host; hostPk.location = locationName; String hostUser = PoseidonPropertyService.getProperty("dist.host.user"); Integer hostActive = Integer.valueOf(PoseidonPropertyService.getProperty("dist.host.active")); Integer hostPriotity = Integer.valueOf(PoseidonPropertyService.getProperty("dist.host.priority")); host = DistLocationHost.find.byId(hostPk); if (host == null) { host = new DistLocationHost(); host.pk = hostPk; host.user = hostUser; host.active = hostActive; host.priority = hostPriotity; logger.info("DistLocationHost med id {} ikke funnet, oppretter ny {}", hostPk, host); host.insert(DIST_SERVER); } else { host.user = hostUser; host.active = hostActive; host.priority = hostPriotity; logger.debug("DistLocationHost med id {} funnet, oppdaterer med {}", hostPk, host); host.update(DIST_SERVER); } } private void saveProduct(String productName, Long orderId) { logger.debug("saving product {}", productName); DistProduct distProduct = DistProduct.find.byId(productName); String fileName = PoseidonPropertyService.getProperty("dist.product.fileprefix") + orderId + "[._].+"; String type = PoseidonPropertyService.getProperty("dist.product.type"); if (distProduct == null) { distProduct = new DistProduct(); distProduct.name = productName; distProduct.file = fileName; distProduct.type = type; logger.info("DistProduct med id {} ikke funnet, oppretter ny {}",productName,distProduct); distProduct.insert(DIST_SERVER); } else { distProduct.file = fileName; distProduct.type = type; logger.debug("DistProduct med id {} funnet, oppdaterer med {}",productName,distProduct); distProduct.update(DIST_SERVER); } } private void saveDistProductLocation(String productName) { DistProductLocationsPK pk = new DistProductLocationsPK(); pk.product = productName; pk.location = productName; DistProductLocation pl = DistProductLocation.find.byId(pk); if (pl == null) { pl = new DistProductLocation(); pl.pk = pk; pl.processing = productName; logger.info("DistProductLocation med id {} ikke funnet, oppretter ny {}",pk,pl); pl.insert(DIST_SERVER); } else { pl.processing = productName; logger.debug("DistProductLocation med id {} funnet, oppdaterer med {}",pk,pl); pl.update(DIST_SERVER); } } private void saveProcessing(String name) { logger.debug("saving processing {}", name); DistProcessing proc = DistProcessing.find.byId(name); String cmd = PoseidonPropertyService.getProperty("dist.processing.script") + " " + name; if (proc == null) { proc = new DistProcessing(); proc.name = name; proc.command = cmd; proc.priority = 1; proc.pre_post = "pre"; proc.loc_rem = "remote"; logger.info("DistProcessing med navn '{}' ikke funnet. Oppretter ny", name); proc.insert(DIST_SERVER); } else { proc.command = cmd; proc.priority = 1; proc.pre_post = "pre"; proc.loc_rem = "remote"; logger.debug("DistProcessing med navn '{}' funnet. Oppdaterer", name); proc.update(DIST_SERVER); } } }