Attached Files | 39878.patch [^] (26,837 bytes) 2019-01-09 18:14 [Show Content] [Hide Content]# HG changeset patch
# User Javier Armendáriz <javier.armendariz@openbravo.com>
# Date 1546945152 -3600
# Tue Jan 08 11:59:12 2019 +0100
# Node ID 67ad222166928f0deedd468e58f2165c7d5d7be4
# Parent e3f5cce51a50f20963a2179a56d9e49f1d11d54a
Fixed issue 39878: Import entry can be created defining Client and Organization.
Created a Builder class with fluent API to create new Import entries that
validates and creates the new object. If commit and close is set to true,
ImportEntryManager will be notified about the new entry created.
diff --git a/src-test/src/org/openbravo/test/AllAntTaskTests.java b/src-test/src/org/openbravo/test/AllAntTaskTests.java
--- a/src-test/src/org/openbravo/test/AllAntTaskTests.java
+++ b/src-test/src/org/openbravo/test/AllAntTaskTests.java
@@ -11,7 +11,7 @@
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2009-2018 Openbravo SLU
+ * All portions are Copyright (C) 2009-2019 Openbravo SLU
* All Rights Reserved.
* Contributor(s): ______________________________________.
************************************************************************
@@ -113,6 +113,7 @@
import org.openbravo.test.system.CryptoUtilities;
import org.openbravo.test.system.ErrorTextParserIntegrationTest;
import org.openbravo.test.system.ErrorTextParserTest;
+import org.openbravo.test.system.ImportEntryBuilderTest;
import org.openbravo.test.system.ImportEntrySizeTest;
import org.openbravo.test.system.Issue29934Test;
import org.openbravo.test.system.Sessions;
@@ -210,6 +211,7 @@
TestInfrastructure.class, //
Issue29934Test.class, //
ImportEntrySizeTest.class, //
+ ImportEntryBuilderTest.class, //
CryptoUtilities.class, //
Sessions.class, //
OBContextTest.class, //
diff --git a/src-test/src/org/openbravo/test/system/ImportEntryBuilderTest.java b/src-test/src/org/openbravo/test/system/ImportEntryBuilderTest.java
new file mode 100644
--- /dev/null
+++ b/src-test/src/org/openbravo/test/system/ImportEntryBuilderTest.java
@@ -0,0 +1,263 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo Public License
+ * Version 1.1 (the "License"), being the Mozilla Public License
+ * Version 1.1 with a permitted attribution clause; you may not use this
+ * file except in compliance with the License. You may obtain a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2019 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s): ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.test.system;
+
+import static junit.framework.TestCase.assertFalse;
+import static junit.framework.TestCase.assertTrue;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.util.Date;
+
+import org.apache.commons.lang.StringUtils;
+import org.hibernate.Hibernate;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.base.provider.OBProvider;
+import org.openbravo.base.weld.test.WeldBaseTest;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.ad.access.Role;
+import org.openbravo.model.ad.system.Client;
+import org.openbravo.model.common.enterprise.Organization;
+import org.openbravo.service.importprocess.ImportEntry;
+import org.openbravo.service.importprocess.ImportEntryAlreadyExistsException;
+import org.openbravo.service.importprocess.ImportEntryArchive;
+import org.openbravo.service.importprocess.ImportEntryBuilder;
+
+/**
+ * Test the ImportEntryBuilder under various scenarios
+ *
+ * @author jarmendariz
+ */
+public class ImportEntryBuilderTest extends WeldBaseTest {
+
+ private static final Organization F_B_SPAIN_NORTH_ORG = OBDal.getInstance().getProxy(
+ Organization.class, "E443A31992CB4635AFCAEABE7183CE85");
+ private static final Organization SPAIN_ORG = OBDal.getInstance().getProxy(Organization.class,
+ "357947E87C284935AD1D783CF6F099A1");
+ private static final Client QA_TESTING_CLIENT = OBDal.getInstance().getProxy(Client.class,
+ "4028E6C72959682B01295A070852010D");
+ private static final Client F_B_INTL_CLIENT = OBDal.getInstance().getProxy(Client.class,
+ "23C59575B9CF467C9620760EB255B389");
+ private static final Role QA_TESTING_ADMIN_ROLE = OBDal.getInstance().getProxy(Role.class,
+ "4028E6C72959682B01295A071429011E");
+ private static final String JSON_DATA = "{\"custom\":\"data\"}";
+ private static final String IMPORT_STATUS = "Initial";
+ private static final String TYPE_OF_DATA = "Order"; // Available values can be found in
+ // POSImportEntryProcessor.POSTTYPEOFDATA
+
+ private ImportEntry createdEntry;
+ private static ImportEntry existingImportEntry;
+ private static ImportEntryArchive existingImportEntryArchive;
+
+ /**
+ * Create two sample import entries. One for ImportEntry table and the other for
+ * ImportEntryArchive Those entries will be removed in @AfterClass
+ */
+ @BeforeClass
+ public static void createImportEntries() {
+ OBContext.setOBContext("100", "0", TEST_CLIENT_ID, TEST_ORG_ID);
+
+ existingImportEntry = createImportEntry();
+ existingImportEntryArchive = createImportEntryArchive();
+
+ Hibernate.initialize(SPAIN_ORG);
+ Hibernate.initialize(F_B_SPAIN_NORTH_ORG);
+ Hibernate.initialize(QA_TESTING_CLIENT);
+ Hibernate.initialize(F_B_INTL_CLIENT);
+ Hibernate.initialize(QA_TESTING_ADMIN_ROLE);
+
+ OBDal.getInstance().save(existingImportEntry);
+ OBDal.getInstance().save(existingImportEntryArchive);
+ OBDal.getInstance().commitAndClose();
+ }
+
+ private static ImportEntryArchive createImportEntryArchive() {
+ ImportEntryArchive importEntryArchive = OBProvider.getInstance().get(ImportEntryArchive.class);
+ importEntryArchive.setNewOBObject(true);
+ importEntryArchive.setJsonInfo(JSON_DATA);
+ importEntryArchive.setImported(new Date());
+ importEntryArchive.setImportStatus(IMPORT_STATUS);
+ importEntryArchive.setTypeofdata(TYPE_OF_DATA);
+
+ return importEntryArchive;
+ }
+
+ private static ImportEntry createImportEntry() {
+ ImportEntry importEntry = OBProvider.getInstance().get(ImportEntry.class);
+ importEntry.setNewOBObject(true);
+ importEntry.setJsonInfo(JSON_DATA);
+ importEntry.setImported(new Date());
+ importEntry.setImportStatus(IMPORT_STATUS);
+ importEntry.setTypeofdata(TYPE_OF_DATA);
+
+ return importEntry;
+ }
+
+ /**
+ * Check that the new import entry returned contains the inserted data
+ */
+ @Test
+ public void whenNoIdIsProvidedANewEntryIsCreated() {
+ createdEntry = ImportEntryBuilder.newInstance(TYPE_OF_DATA, JSON_DATA) //
+ .create();
+
+ assertThat("Created object has JSON data", createdEntry.getJsonInfo(), is(JSON_DATA));
+ }
+
+ /**
+ * Before creating an ImportEntry, if ID is provided, builded will check whether this entry exists
+ * in ImportEntry table
+ */
+ @Test(expected = ImportEntryAlreadyExistsException.class)
+ public void whenGivenIdExistsImportEntryBuilderThrowsException() {
+ createdEntry = ImportEntryBuilder.newInstance(TYPE_OF_DATA, JSON_DATA) //
+ .setId(existingImportEntry.getId()) //
+ .create();
+ }
+
+ /**
+ * Before creating an ImportEntry, if ID is provided, builded will check whether this entry exists
+ * in ImportEntryArchive table
+ */
+ @Test(expected = ImportEntryAlreadyExistsException.class)
+ public void whenGivenIdExistsAsArchivedImportEntryBuilderThrowsException() {
+ createdEntry = ImportEntryBuilder.newInstance(TYPE_OF_DATA, JSON_DATA) //
+ .setId(existingImportEntryArchive.getId()) //
+ .create();
+ }
+
+ /**
+ * Any field can be filled using .setProperty method. Test that active field can be set using this
+ * method
+ */
+ @Test
+ public void customPropertiesCanBeInserted() {
+ createdEntry = ImportEntryBuilder.newInstance(TYPE_OF_DATA, JSON_DATA) //
+ .setProperty("active", false) //
+ .create();
+
+ assertFalse("Created object should be inactive", createdEntry.isActive());
+ }
+
+ /**
+ * Check that the builder throws an exception when attempting to set a non existing field in
+ * setProperties
+ */
+ @Test(expected = OBException.class)
+ public void whenCustomPropertyToInsertDoesNotExistImportEntryCreationFails() {
+ createdEntry = ImportEntryBuilder.newInstance(TYPE_OF_DATA, JSON_DATA) //
+ .setProperty("customData", "") //
+ .create();
+ }
+
+ /**
+ * Before creating the import entry, is is checked that the client/organization provided matches,
+ * throwing an exception otherwise
+ */
+ @Test(expected = OBException.class)
+ public void whenOrganizationAndClientDoesNotMatchImportEntryCreationFails() {
+ createdEntry = ImportEntryBuilder.newInstance(TYPE_OF_DATA, JSON_DATA) //
+ .setClient(QA_TESTING_CLIENT) //
+ .setOrganization(F_B_SPAIN_NORTH_ORG) //
+ .create();
+ }
+
+ /**
+ * When creating ImportEntries, test that Role are validated
+ */
+ @Test(expected = OBException.class)
+ public void whenRoleAndClientDoesNotMatchImportEntryCreationFails() {
+ createdEntry = ImportEntryBuilder.newInstance(TYPE_OF_DATA, JSON_DATA) //
+ .setClient(F_B_INTL_CLIENT) //
+ .setRole(QA_TESTING_ADMIN_ROLE) //
+ .create();
+ }
+
+ /**
+ * Test that default values are set to the ImportEntry
+ */
+ @Test
+ public void whenNoParametersAreSetImportEntryIsCreatedWithDefaultValues() {
+ createdEntry = ImportEntryBuilder.newInstance(TYPE_OF_DATA, JSON_DATA) //
+ .create();
+
+ Client client = OBContext.getOBContext().getCurrentClient();
+ Organization organization = OBContext.getOBContext().getCurrentOrganization();
+ Role role = OBContext.getOBContext().getRole();
+ assertTrue("Created entry values matches default values",
+ checkImportEntryValues(createdEntry, client, organization, role, TYPE_OF_DATA, JSON_DATA));
+ }
+
+ /**
+ * Test that defined parameters are actually in the created object
+ */
+ @Test
+ public void whenParametersAreSetImportEntryIsCreatedUsingThoseValues() {
+ createdEntry = ImportEntryBuilder.newInstance(TYPE_OF_DATA, JSON_DATA) //
+ .setClient(QA_TESTING_CLIENT) //
+ .setOrganization(SPAIN_ORG) //
+ .setRole(QA_TESTING_ADMIN_ROLE) //
+ .create();
+
+ assertTrue(
+ "Created entry values matches defined values",
+ checkImportEntryValues(createdEntry, QA_TESTING_CLIENT, SPAIN_ORG, QA_TESTING_ADMIN_ROLE,
+ TYPE_OF_DATA, JSON_DATA));
+ }
+
+ private boolean checkImportEntryValues(ImportEntry entry, Client client,
+ Organization organization, Role role, String typeOfData, String jsonData) {
+ return (client.getId().equals(entry.getClient().getId()))
+ && (organization.getId().equals(entry.getOrganization().getId()))
+ && (role.getId().equals(entry.getRole().getId()))
+ && (StringUtils.equals(typeOfData, entry.getTypeofdata()))
+ && (StringUtils.equals(jsonData, entry.getJsonInfo()));
+ }
+
+ @After
+ public void removeImportEntryWhenCreated() {
+ OBContext.setAdminMode(false);
+ try {
+ if (createdEntry != null) {
+ OBDal.getInstance().remove(createdEntry);
+ OBDal.getInstance().flush();
+ }
+ createdEntry = null;
+ } finally {
+ OBContext.restorePreviousMode();
+ }
+
+ }
+
+ @AfterClass
+ public static void disposeImportEntries() {
+ OBContext.setOBContext("100", "0", TEST_CLIENT_ID, TEST_ORG_ID);
+
+ OBDal.getInstance().remove(existingImportEntry);
+ OBDal.getInstance().remove(existingImportEntryArchive);
+ OBDal.getInstance().flush();
+ OBDal.getInstance().commitAndClose();
+ }
+
+}
diff --git a/src/org/openbravo/service/importprocess/ImportEntryAlreadyExistsException.java b/src/org/openbravo/service/importprocess/ImportEntryAlreadyExistsException.java
new file mode 100644
--- /dev/null
+++ b/src/org/openbravo/service/importprocess/ImportEntryAlreadyExistsException.java
@@ -0,0 +1,41 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo Public License
+ * Version 1.1 (the "License"), being the Mozilla Public License
+ * Version 1.1 with a permitted attribution clause; you may not use this
+ * file except in compliance with the License. You may obtain a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2019 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s): ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.service.importprocess;
+
+import org.openbravo.base.exception.OBException;
+
+public class ImportEntryAlreadyExistsException extends OBException {
+ private static final long serialVersionUID = 1L;
+
+ public ImportEntryAlreadyExistsException() {
+ super();
+ }
+
+ public ImportEntryAlreadyExistsException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public ImportEntryAlreadyExistsException(String message) {
+ super(message);
+ }
+
+ public ImportEntryAlreadyExistsException(Throwable cause) {
+ super(cause);
+ }
+}
diff --git a/src/org/openbravo/service/importprocess/ImportEntryBuilder.java b/src/org/openbravo/service/importprocess/ImportEntryBuilder.java
new file mode 100644
--- /dev/null
+++ b/src/org/openbravo/service/importprocess/ImportEntryBuilder.java
@@ -0,0 +1,234 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo Public License
+ * Version 1.1 (the "License"), being the Mozilla Public License
+ * Version 1.1 with a permitted attribution clause; you may not use this
+ * file except in compliance with the License. You may obtain a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2019 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s): ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.service.importprocess;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.hibernate.query.Query;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.base.model.Entity;
+import org.openbravo.base.model.ModelProvider;
+import org.openbravo.base.provider.OBProvider;
+import org.openbravo.base.weld.WeldUtils;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.core.SessionHandler;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.ad.access.Role;
+import org.openbravo.model.ad.system.Client;
+import org.openbravo.model.common.enterprise.Organization;
+
+/**
+ * Builder class used to easily create a new ImportEntry instance
+ */
+public class ImportEntryBuilder {
+
+ private static final Logger log = LogManager.getLogger();
+
+ private final String INITIAL_IMPORT_STATUS = "Initial";
+
+ private List<ImportEntryPreProcessor> entryPreProcessors;
+
+ private String id;
+ private Client client;
+ private Organization organization;
+ private Role role;
+ private String typeOfData;
+ private String jsonData;
+ private Map<String, Object> customProperties;
+ private boolean commitAndClose;
+
+ public static ImportEntryBuilder newInstance(String typeOfData, String jsonData) {
+ return new ImportEntryBuilder(typeOfData, jsonData);
+ }
+
+ private ImportEntryBuilder(String typeOfData, String jsonData) {
+ this.client = OBContext.getOBContext().getCurrentClient();
+ this.organization = OBContext.getOBContext().getCurrentOrganization();
+ this.role = OBContext.getOBContext().getRole();
+ this.customProperties = new HashMap<>();
+ this.commitAndClose = false;
+ this.typeOfData = typeOfData;
+ this.jsonData = jsonData;
+ }
+
+ public ImportEntryBuilder setId(String id) {
+ this.id = id;
+ return this;
+ }
+
+ public ImportEntryBuilder setOrganization(Organization organization) {
+ this.organization = organization;
+ return this;
+ }
+
+ public ImportEntryBuilder setClient(Client client) {
+ this.client = client;
+ return this;
+ }
+
+ public ImportEntryBuilder setRole(Role role) {
+ this.role = role;
+ return this;
+ }
+
+ public ImportEntryBuilder setProperty(String propertyName, Object propertyValue) {
+ this.customProperties.put(propertyName, propertyValue);
+ return this;
+ }
+
+ public ImportEntryBuilder setCommitAndClose(boolean commitAndClose) {
+ this.commitAndClose = commitAndClose;
+ return this;
+ }
+
+ /**
+ * Creates a ImportEntry instance, checks that this instance is not already created or archived
+ * and then saves it. If setCommitAndClose(true) is called, the transaction is commited and then
+ * the connection is closed.
+ *
+ * @throws ImportEntryAlreadyExistsException
+ * when import entry already exists either in c_import_entry or c_import_entry_archive
+ * tables
+ * @throws OBException
+ * when data provided is not valid. This could be caused because: Client/Organization
+ * does not match, role is not part of client or attempting to insert a non-existing
+ * custom property
+ */
+ public ImportEntry create() {
+ OBContext.setAdminMode(false);
+ try {
+ validateImportEntryData();
+ ImportEntry importEntry = createImportEntry();
+ processAndSave(importEntry, commitAndClose);
+ return importEntry;
+ } finally {
+ OBContext.restorePreviousMode();
+ }
+ }
+
+ private void validateImportEntryData() {
+ if (importEntryAlreadyExists(id)) {
+ log.debug("Entry already exists, ignoring it, id/typeofdata {}/{} json {}", id, typeOfData,
+ jsonData);
+ throw new ImportEntryAlreadyExistsException("ImportEntry already exists");
+ }
+
+ if (importEntryIsAlreadyArchived(id)) {
+ log.debug("Entry already archived, ignoring it, id/typeofdata {}/{}" + id + "/" + typeOfData
+ + " json " + jsonData);
+ throw new ImportEntryAlreadyExistsException(
+ "ImportEntry already exists in ImportEntryArchive");
+ }
+
+ if (!organization.getClient().getId().equals(client.getId())) {
+ log.debug("Organization {} is not matching with client {}", organization.getName(),
+ client.getName());
+ throw new OBException("Client/Organization mismatch");
+ }
+
+ if (!role.getClient().getId().equals(client.getId())) {
+ log.debug("Role {} is not matching with client {}", role.getName(), client.getName());
+ throw new OBException("Role/Client mismatch");
+ }
+
+ Entity entity = ModelProvider.getInstance().getEntity(ImportEntry.class);
+ for (String property : customProperties.keySet()) {
+ if (!entity.hasProperty(property)) {
+ log.debug("Attempting to set non-existing property {} in ImportEntry", property);
+ throw new OBException("Property " + property + " cannot be found");
+ }
+ }
+ }
+
+ private ImportEntry createImportEntry() {
+ ImportEntry importEntry = OBProvider.getInstance().get(ImportEntry.class);
+ if (id != null) {
+ importEntry.setId(id);
+ }
+
+ importEntry.setNewOBObject(true);
+ importEntry.setCreatedtimestamp((new Date()).getTime());
+ importEntry.setImportStatus(INITIAL_IMPORT_STATUS);
+ importEntry.setClient(client);
+ importEntry.setOrganization(organization);
+ importEntry.setRole(role);
+ importEntry.setJsonInfo(jsonData);
+ importEntry.setTypeofdata(typeOfData);
+
+ for (Map.Entry<String, Object> property : customProperties.entrySet()) {
+ importEntry.set(property.getKey(), property.getValue());
+ }
+
+ return importEntry;
+ }
+
+ private boolean importEntryAlreadyExists(String entryId) {
+ if (StringUtils.isEmpty(entryId)) {
+ return false;
+ }
+
+ final Query<Number> qry = SessionHandler
+ .getInstance()
+ .getSession()
+ .createQuery("select count(*) from " + ImportEntry.ENTITY_NAME + " where id=:id",
+ Number.class);
+ qry.setParameter("id", entryId);
+
+ return qry.uniqueResult().intValue() > 0;
+ }
+
+ private boolean importEntryIsAlreadyArchived(String entryId) {
+ if (StringUtils.isEmpty(entryId)) {
+ return false;
+ }
+
+ final Query<Number> qry = SessionHandler
+ .getInstance()
+ .getSession()
+ .createQuery("select count(*) from " + ImportEntryArchive.ENTITY_NAME + " where id=:id",
+ Number.class);
+ qry.setParameter("id", entryId);
+
+ return qry.uniqueResult().intValue() > 0;
+ }
+
+ private void processAndSave(ImportEntry importEntry, boolean commitClose) {
+ for (ImportEntryPreProcessor processor : getEntryPreProcessors()) {
+ processor.beforeCreate(importEntry);
+ }
+ OBDal.getInstance().save(importEntry);
+ if (commitClose) {
+ OBDal.getInstance().commitAndClose();
+ ImportEntryManager.getInstance().notifyNewImportEntryCreated();
+ }
+ }
+
+ private List<ImportEntryPreProcessor> getEntryPreProcessors() {
+ if (entryPreProcessors == null) {
+ entryPreProcessors = WeldUtils.getInstances(ImportEntryPreProcessor.class);
+ }
+ return entryPreProcessors;
+ }
+}
diff --git a/src/org/openbravo/service/importprocess/ImportEntryManager.java b/src/org/openbravo/service/importprocess/ImportEntryManager.java
--- a/src/org/openbravo/service/importprocess/ImportEntryManager.java
+++ b/src/org/openbravo/service/importprocess/ImportEntryManager.java
@@ -11,7 +11,7 @@
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2015-2018 Openbravo SLU
+ * All portions are Copyright (C) 2015-2019 Openbravo SLU
* All Rights Reserved.
* Contributor(s): ______________________________________.
************************************************************************
@@ -47,12 +47,9 @@
import org.hibernate.ScrollableResults;
import org.hibernate.query.Query;
import org.openbravo.base.exception.OBException;
-import org.openbravo.base.provider.OBProvider;
import org.openbravo.base.session.OBPropertiesProvider;
import org.openbravo.dal.core.OBContext;
-import org.openbravo.dal.core.SessionHandler;
import org.openbravo.dal.service.OBDal;
-import org.openbravo.model.ad.access.Role;
import org.openbravo.service.json.JsonUtils;
/**
@@ -302,59 +299,14 @@
* Note will commit the session/connection using {@link OBDal#commitAndClose()}
*/
public void createImportEntry(String id, String typeOfData, String json, boolean commitAndClose) {
- OBContext.setAdminMode(true);
try {
- // check if it is not there already or already archived
- {
- final Query<Number> qry = SessionHandler
- .getInstance()
- .getSession()
- .createQuery("select count(*) from " + ImportEntry.ENTITY_NAME + " where id=:id",
- Number.class);
- qry.setParameter("id", id);
- if (qry.uniqueResult().intValue() > 0) {
- log.debug("Entry already exists, ignoring it, id/typeofdata " + id + "/" + typeOfData
- + " json " + json);
- return;
- }
- }
- {
- final Query<Number> qry = SessionHandler
- .getInstance()
- .getSession()
- .createQuery(
- "select count(*) from " + ImportEntryArchive.ENTITY_NAME + " where id=:id",
- Number.class);
- qry.setParameter("id", id);
- if (qry.uniqueResult().intValue() > 0) {
- log.debug("Entry already archived, ignoring it, id/typeofdata " + id + "/" + typeOfData
- + " json " + json);
- return;
- }
- }
-
- ImportEntry importEntry = OBProvider.getInstance().get(ImportEntry.class);
- importEntry.setId(id);
- importEntry.setRole(OBDal.getInstance().getProxy(Role.class,
- OBContext.getOBContext().getRole().getId()));
- importEntry.setNewOBObject(true);
- importEntry.setImportStatus("Initial");
- importEntry.setCreatedtimestamp((new Date()).getTime());
- importEntry.setImported(null);
- importEntry.setTypeofdata(typeOfData);
- importEntry.setJsonInfo(json);
-
- for (ImportEntryPreProcessor processor : entryPreProcessors) {
- processor.beforeCreate(importEntry);
- }
- OBDal.getInstance().save(importEntry);
- if (commitAndClose) {
- OBDal.getInstance().commitAndClose();
-
- notifyNewImportEntryCreated();
- }
- } finally {
- OBContext.restorePreviousMode();
+ ImportEntryBuilder.newInstance(typeOfData, json) //
+ .setId(id) //
+ .setCommitAndClose(commitAndClose) //
+ .create();
+ } catch (ImportEntryAlreadyExistsException e) {
+ // Ignore exception when ImportEntry already exists either in ImportEntry or
+ // ImportEntryArchive table
}
}
|