/
RepelnishmentImportTableSqlUpdater.java
194 lines (174 loc) · 8.04 KB
/
RepelnishmentImportTableSqlUpdater.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package de.metas.replenishment.impexp;
import static org.adempiere.impexp.AbstractImportProcess.COLUMNNAME_I_ErrorMsg;
import static org.adempiere.impexp.AbstractImportProcess.COLUMNNAME_I_IsImported;
import org.adempiere.ad.trx.api.ITrx;
import org.compiere.model.I_C_Period;
import org.compiere.model.I_I_Replenish;
import org.compiere.model.I_M_Replenish;
import org.compiere.util.DB;
import org.slf4j.Logger;
import de.metas.adempiere.model.I_M_Product;
import de.metas.logging.LogManager;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
/*
* #%L
* de.metas.adempiere.adempiere.base
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
/**
* A helper class for {@link ReplenishmentImportProcess} that updates {@link I_I_Replenish} table.
*
* @author metas-dev <dev@metasfresh.com>
*
*/
@UtilityClass
public class RepelnishmentImportTableSqlUpdater
{
private static final transient Logger logger = LogManager.getLogger(RepelnishmentImportTableSqlUpdater.class);
public void updateReplenishmentImortTable(@NonNull final String whereClause)
{
dbUpdateOrg(whereClause);
dbUpdateProducIds(whereClause);
dbUpdateWarehouse(whereClause);
dbUpdateSourceWarehouse(whereClause);
dbUpdateLocators(whereClause);
dbUpdatePeriodIds(whereClause);
//
dbUpdateReplenishments(whereClause);
dbUpdateErrorMessages(whereClause);
}
private void dbUpdateProducIds(final String whereClause)
{
StringBuilder sql;
int no;
sql = new StringBuilder("UPDATE " + I_I_Replenish.Table_Name + " i ")
.append("SET M_Product_ID=(SELECT M_Product_ID FROM M_Product p ")
.append("WHERE i." + I_I_Replenish.COLUMNNAME_ProductValue)
.append("=p." + I_M_Product.COLUMNNAME_Value)
.append(" AND p.AD_Client_ID=i.AD_Client_ID ")
.append(" AND p.IsActive='Y') ")
.append("WHERE M_Product_ID IS NULL AND " + I_I_Replenish.COLUMNNAME_ProductValue + " IS NOT NULL")
.append(" AND " + COLUMNNAME_I_IsImported + "<>'Y' ")
.append(whereClause);
no = DB.executeUpdateEx(sql.toString(), ITrx.TRXNAME_ThreadInherited);
logger.info("Found Products={}", no);
}
private void dbUpdateOrg(@NonNull final String whereClause)
{
final StringBuilder sql = new StringBuilder("UPDATE " + I_I_Replenish.Table_Name + " i ")
.append("SET AD_Org_ID=(SELECT AD_Org_ID FROM AD_org o WHERE o.value = ")
.append(I_I_Replenish.COLUMNNAME_OrgValue)
.append(" ) WHERE AD_Org_ID IS NULL AND OrgValue IS NOT NULL ")
.append("AND I_IsImported<>'Y' ")
.append(whereClause);
DB.executeUpdateEx(sql.toString(), ITrx.TRXNAME_ThreadInherited);
}
private void dbUpdateWarehouse(@NonNull final String whereClause)
{
final StringBuilder sql = new StringBuilder("UPDATE " + I_I_Replenish.Table_Name + " i ")
.append("SET M_Warehouse_ID=(SELECT M_Warehouse_ID FROM M_Warehouse w WHERE w.value = ")
.append(I_I_Replenish.COLUMNNAME_WarehouseValue)
.append(" ) WHERE M_Warehouse_ID IS NULL AND WarehouseValue IS NOT NULL ")
.append("AND I_IsImported<>'Y' ")
.append(whereClause);
DB.executeUpdateEx(sql.toString(), ITrx.TRXNAME_ThreadInherited);
}
private void dbUpdateSourceWarehouse(@NonNull final String whereClause)
{
final StringBuilder sql = new StringBuilder("UPDATE " + I_I_Replenish.Table_Name + " i ")
.append("SET M_WarehouseSource_ID=(SELECT M_Warehouse_ID FROM M_Warehouse w WHERE w.value = ")
.append(I_I_Replenish.COLUMNNAME_WarehouseSourceValue)
.append(" ) WHERE M_WarehouseSource_ID IS NULL AND WarehouseSourceValue IS NOT NULL ")
.append("AND I_IsImported<>'Y' ")
.append(whereClause);
DB.executeUpdateEx(sql.toString(), ITrx.TRXNAME_ThreadInherited);
}
private void dbUpdateLocators(@NonNull final String whereClause)
{
StringBuilder sql = new StringBuilder("UPDATE " + I_I_Replenish.Table_Name + " i ")
.append("SET M_Locator_ID=(SELECT MAX(M_Locator_ID) FROM M_Locator l ")
.append("WHERE i.LocatorValue=l.Value AND i.M_Warehouse_ID = l.M_Warehouse_ID AND i.AD_Client_ID=l.AD_Client_ID) ")
.append("WHERE M_Locator_ID IS NULL AND LocatorValue IS NOT NULL ")
.append("AND I_IsImported<>'Y' ")
.append(whereClause);
DB.executeUpdateEx(sql.toString(), ITrx.TRXNAME_ThreadInherited);
}
private void dbUpdatePeriodIds(final String whereClause)
{
StringBuilder sql;
int no;
sql = new StringBuilder("UPDATE " + I_I_Replenish.Table_Name + " i ")
.append("SET C_Period_ID=(SELECT C_Period_ID FROM C_Period p ")
.append("WHERE i." + I_I_Replenish.COLUMNNAME_DateGeneral)
.append(">=p." + I_C_Period.COLUMNNAME_StartDate)
.append(" AND i." + I_I_Replenish.COLUMNNAME_DateGeneral)
.append("<=p." + I_C_Period.COLUMNNAME_EndDate)
.append(" AND p.AD_Client_ID=i.AD_Client_ID ")
.append(" AND p.IsActive='Y') ")
.append("WHERE " + I_I_Replenish.COLUMNNAME_DateGeneral + " IS NOT NULL")
.append(" AND " + COLUMNNAME_I_IsImported + "<>'Y' ")
.append(whereClause);
no = DB.executeUpdateEx(sql.toString(), ITrx.TRXNAME_ThreadInherited);
logger.info("Found Products={}", no);
}
private void dbUpdateReplenishments(final String whereClause)
{
StringBuilder sql;
int no;
sql = new StringBuilder("UPDATE " + I_I_Replenish.Table_Name + " i ")
.append("SET M_Replenish_ID=(SELECT M_Replenish_ID FROM M_Replenish r ")
.append("WHERE i." + I_I_Replenish.COLUMNNAME_M_Product_ID)
.append("=r." + I_M_Replenish.COLUMNNAME_M_Product_ID)
.append(" AND i." + I_I_Replenish.COLUMNNAME_M_Warehouse_ID)
.append("=r." + I_M_Replenish.COLUMNNAME_M_Warehouse_ID)
.append(" AND coalesce(i." + I_I_Replenish.COLUMNNAME_M_WarehouseSource_ID)
.append(",1)=coalesce(r." + I_M_Replenish.COLUMNNAME_M_WarehouseSource_ID+",1)")
.append(" AND coalesce(i." + I_I_Replenish.COLUMNNAME_C_Calendar_ID)
.append(",1)=coalesce(r." + I_M_Replenish.COLUMNNAME_C_Calendar_ID+",1)")
.append(" AND coalesce(i." + I_I_Replenish.COLUMNNAME_C_Period_ID)
.append(",1)=coalesce(r." + I_M_Replenish.COLUMNNAME_C_Period_ID+",1)")
.append(" AND r.AD_Client_ID=i.AD_Client_ID ")
.append(" AND r.IsActive='Y') ")
.append("WHERE M_Replenish_ID IS NULL")
.append(" AND " + COLUMNNAME_I_IsImported + "<>'Y' ")
.append(whereClause);
no = DB.executeUpdateEx(sql.toString(), ITrx.TRXNAME_ThreadInherited);
logger.info("Found Products={}", no);
}
private void dbUpdateErrorMessages(final String whereClause)
{
StringBuilder sql;
int no;
sql = new StringBuilder("UPDATE " + I_I_Replenish.Table_Name)
.append(" SET " + COLUMNNAME_I_IsImported + "='N', " + COLUMNNAME_I_ErrorMsg + "=" + COLUMNNAME_I_ErrorMsg + "||'ERR=Product is mandatory, ' ")
.append("WHERE " + I_I_Replenish.COLUMNNAME_M_Product_ID + " IS NULL " + I_I_Replenish.COLUMNNAME_ProductValue + " IS NOT NULL ")
.append("AND " + COLUMNNAME_I_IsImported + "<>'Y'")
.append(whereClause);
no = DB.executeUpdateEx(sql.toString(), ITrx.TRXNAME_ThreadInherited);
logger.info("Product is mandatory={}", no);
sql = new StringBuilder("UPDATE " + I_I_Replenish.Table_Name)
.append(" SET " + COLUMNNAME_I_IsImported + "='N', " + COLUMNNAME_I_ErrorMsg + "=" + COLUMNNAME_I_ErrorMsg + "||'ERR=Wareouse is mandatory, ' ")
.append("WHERE " + I_I_Replenish.COLUMNNAME_M_Warehouse_ID + " IS NULL " + I_I_Replenish.COLUMNNAME_WarehouseValue + " IS NOT NULL ")
.append("AND " + COLUMNNAME_I_IsImported + "<>'Y'")
.append(whereClause);
no = DB.executeUpdateEx(sql.toString(), ITrx.TRXNAME_ThreadInherited);
logger.info("Warehouse is mandatory={}", no);
}
}