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 = buildFields (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 (fields .keySet ())
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,13 +201,13 @@ 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 = buildFields (tableName , dynamicEntity );
207205
208206 try {
209207 Record record = dsl .update (table )
210208 .set (fields )
211209 .where (DSL .field (idColumn ).eq (id ))
212- .returning (DSL . asterisk ())
210+ .returning (fields . keySet ())
213211 .fetchOne ();
214212
215213 if (record == null ) {
@@ -242,7 +240,8 @@ public DynamicEntity patch(final ProviderConfiguration config,
242240 Record record = dsl .update (table )
243241 .set (fields )
244242 .where (DSL .field (idColumn ).eq (id ))
245- .returning (DSL .asterisk ())
243+ // Rebuild fields to have all fields
244+ .returning (buildFields (tableName , dynamicEntity ).keySet ())
246245 .fetchOne ();
247246
248247 if (record == null ) {
@@ -317,15 +316,14 @@ public Name resolveIdColumn(final DynamicEntity dynamicEntity) {
317316 * @param dynamicEntity the source entity
318317 * @return the map of database fields and values
319318 */
320- public Map <Field <?>, Object > buildFields (final DynamicEntity dynamicEntity ) {
319+ public Map <Field <?>, Object > buildFields (final String tableName , final DynamicEntity dynamicEntity ) {
321320 Map <String , Object > attributes = dynamicEntity .getAttributes ();
322321
323322 return dynamicEntity .getConfiguration ().getAttributes ().stream ()
324- .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" ))),
328- attr -> attributes .getOrDefault (attr .getName (), null )));
325+ attr -> DSL .field (DSL .name (tableName , (String ) attr .getAccess ().get ("column" ))),
326+ attr -> attributes .getOrDefault (attr .getName (), DSL . defaultValue () )));
329327 }
330328
331329 /**
@@ -359,14 +357,13 @@ public DynamicEntity mappingEntity(final Record record, final DynamicEntity dyna
359357 entity .setConfiguration (dynamicEntity .getConfiguration ());
360358 entity .setAttributes (new HashMap <>());
361359
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- });
360+ entity .setAttributes (dynamicEntity .getConfiguration ()
361+ .getAttributes ()
362+ .stream ()
363+ .collect (Collectors .toMap (
364+ AttributeConfiguration ::getName ,
365+ attr -> recordMap .get ((String ) attr .getAccess ().get ("column" ))
366+ )));
370367
371368 return entity ;
372369 }
0 commit comments