2828
2929import io .github .linagora .linid .im .corelib .exception .ApiException ;
3030import io .github .linagora .linid .im .corelib .i18n .I18nMessage ;
31+ import io .github .linagora .linid .im .corelib .plugin .config .dto .AttributeConfiguration ;
3132import io .github .linagora .linid .im .corelib .plugin .config .dto .ProviderConfiguration ;
3233import io .github .linagora .linid .im .corelib .plugin .entity .DynamicEntity ;
3334import io .github .linagora .linid .im .corelib .plugin .task .TaskEngine ;
@@ -164,20 +165,16 @@ public DynamicEntity selectOne(final TaskExecutionContext context,
164165 public DynamicEntity insert (final ProviderConfiguration config ,
165166 final DatabasePluginConfiguration databasePluginConfiguration ,
166167 final DynamicEntity dynamicEntity ) {
167-
168- DSLContext dsl = dslRegistry .getDsl (config );
169168 String tableName = databasePluginConfiguration .getTable ();
170- Table <?> table = DSL .table (DSL .name (tableName ));
171- Map <Field <?>, Object > fields = buildFields (dynamicEntity );
172169
173- System .out .println (fields );
174170 try {
171+ DSLContext dsl = dslRegistry .getDsl (config );
172+ Table <?> table = DSL .table (DSL .name (tableName ));
173+ Map <Field <?>, Object > fields = buildFieldValues (tableName , dynamicEntity );
174+
175175 Record record = dsl .insertInto (table )
176176 .set (fields )
177- // DSL.asterisk allows retrieving all columns of the table (basically does a *
178- // in the query)
179- .returning ()
180- // .returning(DSL.asterisk())
177+ .returning (buildFields (tableName , dynamicEntity ))
181178 .fetchOne ();
182179
183180 if (record == null ) {
@@ -187,6 +184,7 @@ public DynamicEntity insert(final ProviderConfiguration config,
187184
188185 return mappingEntity (record , dynamicEntity );
189186 } catch (Exception e ) {
187+ e .printStackTrace ();
190188 log .error ("Error INSERT on tableName `{}`: {}" , tableName , e .getMessage ());
191189 throw new ApiException (400 ,
192190 I18nMessage .of ("dpp.error.insert" , Map .of ("table" , tableName , "error" , e .getMessage ())));
@@ -203,7 +201,7 @@ public DynamicEntity update(final ProviderConfiguration config,
203201 String tableName = databasePluginConfiguration .getTable ();
204202 Table <?> table = DSL .table (DSL .name (tableName ));
205203 var idColumn = resolveIdColumn (dynamicEntity );
206- Map <Field <?>, Object > fields = buildFields ( dynamicEntity );
204+ Map <Field <?>, Object > fields = buildFieldValues ( tableName , dynamicEntity );
207205
208206 try {
209207 Record record = dsl .update (table )
@@ -236,7 +234,7 @@ public DynamicEntity patch(final ProviderConfiguration config,
236234 String tableName = databasePluginConfiguration .getTable ();
237235 Table <?> table = DSL .table (DSL .name (tableName ));
238236 var idColumn = resolveIdColumn (dynamicEntity );
239- Map <Field <?>, Object > fields = buildPartialFields (dynamicEntity );
237+ Map <Field <?>, Object > fields = buildPartialFieldValues (dynamicEntity );
240238
241239 try {
242240 Record record = dsl .update (table )
@@ -317,24 +315,38 @@ public Name resolveIdColumn(final DynamicEntity dynamicEntity) {
317315 * @param dynamicEntity the source entity
318316 * @return the map of database fields and values
319317 */
320- public Map <Field <?>, Object > buildFields ( final DynamicEntity dynamicEntity ) {
318+ public Map <Field <?>, Object > buildFieldValues ( final String tableName , final DynamicEntity dynamicEntity ) {
321319 Map <String , Object > attributes = dynamicEntity .getAttributes ();
322320
323321 return dynamicEntity .getConfiguration ().getAttributes ().stream ()
324322 .filter (attr -> Boolean .FALSE .equals (attr .getAccess ().get ("primaryKey" )))
325323 .filter (attr -> attr .getAccess ().get ("column" ) != null )
326324 .collect (Collectors .toMap (
327- attr -> DSL .field (DSL .name ((String ) attr .getAccess ().get ("column" ))),
325+ attr -> DSL .field (DSL .name (tableName , (String ) attr .getAccess ().get ("column" ))),
328326 attr -> attributes .getOrDefault (attr .getName (), null )));
329327 }
330328
329+ /**
330+ * Builds a list of fields for a full update/insert operation.
331+ *
332+ * @param tableName the table name
333+ * @param dynamicEntity the source entity
334+ * @return the list of database fields
335+ */
336+ public List <Field <?>> buildFields (final String tableName , final DynamicEntity dynamicEntity ) {
337+ return dynamicEntity .getConfiguration ().getAttributes ().stream ()
338+ .filter (attr -> attr .getAccess ().get ("column" ) != null )
339+ .map (attr -> DSL .field (DSL .name (tableName , (String ) attr .getAccess ().get ("column" ))))
340+ .collect (Collectors .toList ());
341+ }
342+
331343 /**
332344 * Builds a map of fields and values for a partial update (patch).
333345 *
334346 * @param dynamicEntity the source entity
335347 * @return the map of database fields and values
336348 */
337- public Map <Field <?>, Object > buildPartialFields (final DynamicEntity dynamicEntity ) {
349+ public Map <Field <?>, Object > buildPartialFieldValues (final DynamicEntity dynamicEntity ) {
338350 Map <String , Object > attributes = dynamicEntity .getAttributes ();
339351
340352 return dynamicEntity .getConfiguration ().getAttributes ().stream ()
@@ -359,14 +371,13 @@ public DynamicEntity mappingEntity(final Record record, final DynamicEntity dyna
359371 entity .setConfiguration (dynamicEntity .getConfiguration ());
360372 entity .setAttributes (new HashMap <>());
361373
362- System .out .println (recordMap );
363- dynamicEntity .getConfiguration ().getAttributes ().forEach (attr -> {
364- String column = (String ) attr .getAccess ().get ("column" );
365-
366- if (column != null && recordMap .containsKey (column )) {
367- entity .getAttributes ().put (attr .getName (), recordMap .get (column ));
368- }
369- });
374+ entity .setAttributes (dynamicEntity .getConfiguration ()
375+ .getAttributes ()
376+ .stream ()
377+ .collect (Collectors .toMap (
378+ AttributeConfiguration ::getName ,
379+ attr -> recordMap .get ((String ) attr .getAccess ().get ("column" ))
380+ )));
370381
371382 return entity ;
372383 }
0 commit comments