# HG changeset patch
# User Javier Armendáriz <javier.armendariz@openbravo.com>
# Date 1516895909 -3600
#      Thu Jan 25 16:58:29 2018 +0100
# Node ID c2d5534fa771393c0a165f5be2b42093ea4e805c
# Parent  21e9102f2f7d35c80b8c06aa5984cfb761005220
Fixed bug 37700: Link to parent trees does not show non active items.

OBQuery by default filters non active fields. Needed to override this behavior on LinkToParentTreeDatasource to show all items in the tree view.

diff --git a/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/LinkToParentTreeDatasourceService.java b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/LinkToParentTreeDatasourceService.java
--- a/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/LinkToParentTreeDatasourceService.java
+++ b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/LinkToParentTreeDatasourceService.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) 2013-2017 Openbravo SLU
+ * All portions are Copyright (C) 2013-2018 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -261,6 +261,7 @@
     final OBQuery<BaseOBObject> query = OBDal.getInstance().createQuery(entity.getName(),
         whereClause.toString());
 
+    query.setFilterOnActive(false);
     query.setParameters(queryParameters);
 
     final DataToJsonConverter toJsonConverter = OBProvider.getInstance().get(
diff --git a/src-test/src/org/openbravo/test/AllWebserviceTests.java b/src-test/src/org/openbravo/test/AllWebserviceTests.java
--- a/src-test/src/org/openbravo/test/AllWebserviceTests.java
+++ b/src-test/src/org/openbravo/test/AllWebserviceTests.java
@@ -29,6 +29,7 @@
 import org.openbravo.test.datasource.FKDropDownDatasource;
 import org.openbravo.test.datasource.FetchDSNoActiveEntityObjects;
 import org.openbravo.test.datasource.HQLDataSourceTest;
+import org.openbravo.test.datasource.LinkToParentTreeDataSourceTest;
 import org.openbravo.test.datasource.OrganizationSelectorDataSourceTest;
 import org.openbravo.test.datasource.ProductSelectorDataSourceTest;
 import org.openbravo.test.datasource.SelectorFieldPropertySelectorDSTest;
@@ -84,6 +85,8 @@
     EmptyStringWhereAndFilterClauseParameter.class, //
     JSONWebServicesWhereParameter.class, //
     WSReadableClientsTest.class, //
-    UserInfoSessionDataTest.class })
+    UserInfoSessionDataTest.class, //
+    LinkToParentTreeDataSourceTest.class //
+})
 public class AllWebserviceTests {
 }
diff --git a/src-test/src/org/openbravo/test/datasource/LinkToParentTreeDataSourceTest.java b/src-test/src/org/openbravo/test/datasource/LinkToParentTreeDataSourceTest.java
new file mode 100644
--- /dev/null
+++ b/src-test/src/org/openbravo/test/datasource/LinkToParentTreeDataSourceTest.java
@@ -0,0 +1,182 @@
+/*
+ *************************************************************************
+ * 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) 2010-2018 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+
+package org.openbravo.test.datasource;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.openbravo.base.provider.OBProvider;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.common.currency.Currency;
+import org.openbravo.model.common.enterprise.DocumentType;
+import org.openbravo.model.materialmgmt.cost.CostAdjustment;
+import org.openbravo.model.materialmgmt.cost.CostAdjustmentLine;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Test case for LinkToParent tree Datasource
+ *
+ * @author jarmendariz
+ */
+public class LinkToParentTreeDataSourceTest extends BaseDataSourceTestDal {
+
+  private static final Logger log = LoggerFactory.getLogger(LinkToParentTreeDataSourceTest.class);
+
+  private static final int STATUS_OK = 0;
+  private static final int NUMBER_OF_COST_ADJUSTMENT_LINES = 2;
+  private CostAdjustmentTestDataHelper dataHelper;
+
+  private String costAdjustmentId;
+
+  @Before
+  public void setUpCostAdjustmentData() {
+    OBContext.setOBContext("100");
+    this.dataHelper = new CostAdjustmentTestDataHelper();
+
+    CostAdjustment costAdjustment = this.dataHelper.createCostAdjustment();
+    this.dataHelper.createActiveCostAdjustmentLine(costAdjustment);
+    this.dataHelper.createNonActiveCostAdjustmentLine(costAdjustment);
+
+    OBDal.getInstance().commitAndClose();
+
+    this.costAdjustmentId = costAdjustment.getId();
+  }
+
+  /**
+   * Ensure that fetching cost adjustment lines both active and non-active lines are retrieved
+   */
+  @Test
+  public void fetchIncludeNonActiveFields() {
+    assertEquals(NUMBER_OF_COST_ADJUSTMENT_LINES, this.getNumberOfCostAdjustmentLines());
+  }
+
+  @After
+  public void tearDownLinkToParentData() {
+    this.dataHelper.removeAllCostAdjustmentData(this.costAdjustmentId);
+    this.costAdjustmentId = null;
+  }
+
+  private int getNumberOfCostAdjustmentLines() {
+    try {
+      JSONObject response = this.requestCostAdjustmentLines();
+      if (this.isResponseOk(response)) {
+        return this.getNumberOfDataItems(response);
+      } else {
+        log.error("DataSource response has no items");
+        return 0;
+      }
+    } catch (Exception exception) {
+      log.error("Cost Adjustment request from DataSource failed", exception);
+      return 0;
+    }
+  }
+
+  private boolean isResponseOk(JSONObject response) throws JSONException {
+    return response.getInt("status") == STATUS_OK;
+  }
+
+  private int getNumberOfDataItems(JSONObject response) throws JSONException {
+    return response.getJSONArray("data").length();
+  }
+
+  private JSONObject requestCostAdjustmentLines() throws Exception {
+    Map<String, String> params = this.generateCostAdjustmentLinesParams(this.costAdjustmentId);
+
+    return new JSONObject(this.doRequest(
+        "/org.openbravo.service.datasource/610BEAE5E223447DBE6FF672B703F72F", params, 200, "POST"))
+        .getJSONObject("response");
+  }
+
+  private Map<String, String> generateCostAdjustmentLinesParams(String id) {
+    Map<String, String> params = new HashMap<>();
+    params.put("_operationType", "fetch");
+    params.put("_startRow", "0");
+    params.put("_endRow", "200");
+    params.put("referencedTableId", "34E79323CEC847C2A9ED2C8430AC73D1");
+    params.put("parentRecordId", id);
+    params.put("tabId", "06DCB72BB6D24F82BCDA5FFF8EA0425C");
+    params.put("@CostAdjustment.id@", id);
+    params
+        .put(
+            "criteria",
+            "{\"_constructor\":\"AdvancedCriteria\",\"fieldName\":\"parentId\",\"value\":\"-1\",\"operator\":\"equals\"}");
+
+    return params;
+  }
+
+  private class CostAdjustmentTestDataHelper {
+
+    private static final String DOCUMENT_TYPE_ID = "82000D718BDA40C38F83FA1A5FFF6419";
+    private static final String SOURCE_PROCESS = "MCC";
+    private static final String DOCUMENT_ID = "::DOCUMENT-ID::";
+
+    public CostAdjustmentLine createActiveCostAdjustmentLine(CostAdjustment costAdjustment) {
+      return this.createCostAdjustmentLine(costAdjustment, 100L, true);
+    }
+
+    public CostAdjustmentLine createNonActiveCostAdjustmentLine(CostAdjustment costAdjustment) {
+      return this.createCostAdjustmentLine(costAdjustment, 200L, false);
+    }
+
+    public CostAdjustment createCostAdjustment() {
+      OBDal obdal = OBDal.getInstance();
+
+      CostAdjustment costAdjustment = OBProvider.getInstance().get(CostAdjustment.class);
+      costAdjustment.setDocumentType(obdal.get(DocumentType.class, DOCUMENT_TYPE_ID));
+      costAdjustment.setDocumentNo(DOCUMENT_ID);
+      costAdjustment.setSourceProcess(SOURCE_PROCESS);
+
+      obdal.save(costAdjustment);
+
+      return costAdjustment;
+    }
+
+    public void removeAllCostAdjustmentData(String id) {
+      OBDal obdal = OBDal.getInstance();
+      obdal.remove(obdal.get(CostAdjustment.class, id));
+      obdal.commitAndClose();
+    }
+
+    private CostAdjustmentLine createCostAdjustmentLine(CostAdjustment costAdjustment, Long lineNo,
+        boolean isActive) {
+      OBDal obdal = OBDal.getInstance();
+
+      CostAdjustmentLine line = OBProvider.getInstance().get(CostAdjustmentLine.class);
+      line.setLineNo(lineNo);
+      line.setCostAdjustment(costAdjustment);
+      line.setCurrency(obdal.get(Currency.class, EURO_ID));
+      line.setActive(isActive);
+
+      obdal.save(line);
+
+      return line;
+    }
+
+  }
+}
