1717import edu .ie3 .datamodel .io .processor .EntityProcessor ;
1818import edu .ie3 .datamodel .io .processor .ProcessorProvider ;
1919import edu .ie3 .datamodel .io .processor .timeseries .TimeSeriesProcessorKey ;
20- import edu .ie3 .datamodel .models .UniqueEntity ;
20+ import edu .ie3 .datamodel .models .Entity ;
21+ import edu .ie3 .datamodel .models .Entity ;
2122import edu .ie3 .datamodel .models .input .*;
2223import edu .ie3 .datamodel .models .input .connector .*;
2324import edu .ie3 .datamodel .models .input .container .GraphicElements ;
@@ -84,7 +85,7 @@ public void shutdown() {
8485 * executed by a specific {@link EntityProcessor}
8586 * @throws SQLException
8687 */
87- public <C extends UniqueEntity > void persistAll (Collection <C > entities , DbGridMetadata identifier )
88+ public <C extends Entity > void persistAll (Collection <C > entities , DbGridMetadata identifier )
8889 throws SQLException {
8990 // Extract nested entities and add them to the set of entities
9091 Set <C > entitiesToAdd = new HashSet <>(entities ); // entities to persist
@@ -141,7 +142,7 @@ public <C extends InputEntity> void persistAllIgnoreNested(
141142 * @param identifier identifier of the grid
142143 * @throws SQLException
143144 */
144- public <C extends UniqueEntity > void persist (C entity , DbGridMetadata identifier )
145+ public <C extends Entity > void persist (C entity , DbGridMetadata identifier )
145146 throws SQLException {
146147 if (entity instanceof InputEntity inputEntity ) {
147148 persistIncludeNested (inputEntity , identifier );
@@ -170,7 +171,8 @@ public <C extends InputEntity> void persistIgnoreNested(C entity, DbGridMetadata
170171
171172 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
172173
173- protected <C extends UniqueEntity > void persistListIncludeNested (
174+ /*
175+ protected <C extends Entity> void persistListIncludeNested(
174176 List<C> entities, Class<C> cls, DbGridMetadata identifier) throws SQLException {
175177 if (NestedEntity.class.isAssignableFrom(cls)) {
176178 entities.forEach(
@@ -193,14 +195,16 @@ protected <C extends UniqueEntity> void persistListIncludeNested(
193195 }
194196 }
195197
196- public <C extends UniqueEntity > void persistIncludeNested (C entity , DbGridMetadata identifier )
198+ */
199+
200+ public <C extends Entity > void persistIncludeNested (C entity , DbGridMetadata identifier )
197201 throws SQLException {
198202 Set <C > entitiesToAdd = new HashSet <>();
199203 entitiesToAdd .add (entity );
200204 persistAll (entitiesToAdd , identifier );
201205 }
202206
203- private <C extends UniqueEntity > void persistMixedList (
207+ private <C extends Entity > void persistMixedList (
204208 List <C > entities , DbGridMetadata identifier ) {
205209 Map <Class <C >, List <C >> entitiesPerClass =
206210 entities .stream ().collect (groupingBy (entity -> (Class <C >) entity .getClass ()));
@@ -218,17 +222,17 @@ private <C extends UniqueEntity> void persistMixedList(
218222 });
219223 }
220224
221- private <C extends UniqueEntity , E extends TimeSeriesEntry <V >, V extends Value > void persistList (
225+ private <C extends Entity , E extends TimeSeriesEntry <V >, V extends Value > void persistList (
222226 List <C > entities , Class <C > cls , DbGridMetadata identifier ) throws SQLException {
223227 // Check if there are only elements of the same class
224228 Class <?> firstClass = entities .get (0 ).getClass ();
225229 boolean allSameClass = entities .stream ().allMatch (e -> e .getClass () == firstClass );
226230
227231 if (allSameClass ) {
228232 if (InputEntity .class .isAssignableFrom (cls )) {
229- insertListIgnoreNested (entities , cls , identifier );
233+ insertListIgnoreNested (entities , cls , identifier , true );
230234 } else if (ResultEntity .class .isAssignableFrom (cls )) {
231- insertListIgnoreNested (entities , cls , identifier );
235+ insertListIgnoreNested (entities , cls , identifier , false );
232236 } else if (TimeSeries .class .isAssignableFrom (cls )) {
233237 entities .forEach (
234238 ts -> {
@@ -254,15 +258,14 @@ private <C extends UniqueEntity, E extends TimeSeriesEntry<V>, V extends Value>
254258 * Writes a list of entities into a sql table. It's necessary that all entities have the same
255259 * class.
256260 */
257- private <C extends UniqueEntity > void insertListIgnoreNested (
258- List <C > entities , Class <C > cls , DbGridMetadata identifier ) throws SQLException {
261+ private <C extends Entity > void insertListIgnoreNested (
262+ List <C > entities , Class <C > cls , DbGridMetadata identifier , boolean ignoreConflict ) throws SQLException {
259263 try {
260264 String [] headerElements = processorProvider .getHeaderElements (cls );
261265 String query =
262266 basicInsertQueryValuesGrid (
263267 schemaName , databaseNamingStrategy .getEntityName (cls ).orElseThrow (), headerElements );
264- query = query + createInsertQueryBodyIgnoreConflict (entities , headerElements , identifier );
265- System .out .println (query );
268+ query = query + createInsertQueryBodyIgnoreConflict (entities , headerElements , identifier , ignoreConflict );
266269 connector .executeUpdate (query );
267270 } catch (ProcessorProviderException e ) {
268271 log .error ("Exception occurred during processor request: " , e );
@@ -336,15 +339,14 @@ public void persistJointGrid(JointGridContainer jointGridContainer, UUID gridUUI
336339 SystemParticipants systemParticipants = jointGridContainer .getSystemParticipants ();
337340 Set <BmInput > bmPlants = systemParticipants .getBmPlants ();
338341 Set <ChpInput > chpPlants = systemParticipants .getChpPlants ();
339- Set <EvcsInput > evCS = systemParticipants .getEvCS ();
342+ Set <EvcsInput > evCS = systemParticipants .getEvcs ();
340343 Set <EvInput > evs = systemParticipants .getEvs ();
341344 Set <FixedFeedInInput > fixedFeedIns = systemParticipants .getFixedFeedIns ();
342345 Set <HpInput > heatPumps = systemParticipants .getHeatPumps ();
343346 Set <LoadInput > loads = systemParticipants .getLoads ();
344347 Set <PvInput > pvPlants = systemParticipants .getPvPlants ();
345348 Set <StorageInput > storages = systemParticipants .getStorages ();
346349 Set <WecInput > wecPlants = systemParticipants .getWecPlants ();
347- Set <EmInput > emSystems = systemParticipants .getEmSystems ();
348350
349351 // get graphic elements (just for better readability, we could also just get them directly
350352 // below)
@@ -384,14 +386,13 @@ public void persistJointGrid(JointGridContainer jointGridContainer, UUID gridUUI
384386 loads ,
385387 pvPlants ,
386388 storages ,
387- wecPlants ,
388- emSystems )
389+ wecPlants )
389390 .flatMap (Collection ::stream )
390391 .map (Extractor ::extractOperator )
391392 .flatMap (Optional ::stream )
392393 .collect (Collectors .toSet ());
393394
394- List <UniqueEntity > toAdd = new LinkedList <>();
395+ List <Entity > toAdd = new LinkedList <>();
395396 toAdd .addAll (rawGridElements .allEntitiesAsList ());
396397 toAdd .addAll (systemParticipants .allEntitiesAsList ());
397398 toAdd .addAll (graphicElements .allEntitiesAsList ());
@@ -402,7 +403,7 @@ public void persistJointGrid(JointGridContainer jointGridContainer, UUID gridUUI
402403 persistAll (toAdd , identifier );
403404 }
404405
405- private <C extends UniqueEntity > void insert (C entity , DbGridMetadata identifier )
406+ private <C extends Entity > void insert (C entity , DbGridMetadata identifier )
406407 throws SQLException {
407408 try {
408409 String [] headerElements = processorProvider .getHeaderElements (entity .getClass ());
@@ -427,16 +428,24 @@ private <C extends UniqueEntity> void insert(C entity, DbGridMetadata identifier
427428 * will be ignored. Conflicts can occur if an entity (e.g. node) already exist. WARNING: It's
428429 * assumed that all entities are from the same class C.
429430 */
430- private <C extends UniqueEntity > String createInsertQueryBodyIgnoreConflict (
431- List <C > entities , String [] headerElements , DbGridMetadata identifier )
431+ private <C extends Entity > String createInsertQueryBodyIgnoreConflict (
432+ List <C > entities , String [] headerElements , DbGridMetadata identifier , boolean ignoreConflict )
432433 throws ProcessorProviderException {
433434 Set <LinkedHashMap <String , String >> entityFieldData = processorProvider .handleEntities (entities );
434435 String queryBody = "" ;
435- queryBody =
436- queryBody
437- + entityFieldData .stream ()
438- .map (data -> queryValueLine (sqlEntityFieldData (data ), headerElements , identifier ))
439- .collect (Collectors .joining (",\n " , "" , "\n ON CONFLICT (uuid) DO NOTHING;" ));
436+ if (ignoreConflict ) {
437+ queryBody =
438+ queryBody
439+ + entityFieldData .stream ()
440+ .map (data -> queryValueLine (sqlEntityFieldData (data ), headerElements , identifier ))
441+ .collect (Collectors .joining (",\n " , "" , "\n ON CONFLICT (uuid) DO NOTHING;" ));
442+ } else {
443+ queryBody =
444+ queryBody
445+ + entityFieldData .stream ()
446+ .map (data -> queryValueLine (sqlEntityFieldData (data ), headerElements , identifier ))
447+ .collect (Collectors .joining (",\n " , "" , ";\n " ));
448+ }
440449 return queryBody ;
441450 }
442451
@@ -445,7 +454,7 @@ private <C extends UniqueEntity> String createInsertQueryBodyIgnoreConflict(
445454 *
446455 * <p>WARNING: It's assumed that all entities are from the same class C.
447456 */
448- private <C extends UniqueEntity > String createInsertQueryBody (
457+ private <C extends Entity > String createInsertQueryBody (
449458 List <C > entities , String [] headerElements , DbGridMetadata identifier )
450459 throws ProcessorProviderException {
451460 Set <LinkedHashMap <String , String >> entityFieldData = processorProvider .handleEntities (entities );
@@ -472,7 +481,7 @@ private String queryValueLine(
472481 }
473482
474483 /** Creates a line with the values of one entity for an insertion query. */
475- private <C extends UniqueEntity > String queryValueLine (
484+ private <C extends Entity > String queryValueLine (
476485 C entity , String [] headerElements , DbGridMetadata identifier )
477486 throws ProcessorProviderException {
478487 return queryValueLine (
@@ -510,7 +519,9 @@ private static String basicInsertQuery(String schemaName, String tableName) {
510519 /** Provides the insert, column names, grid identifier and the VALUES statement for a query. */
511520 private String basicInsertQueryValuesGrid (
512521 String schemaName , String tableName , String [] headerElements ) {
513- String [] addParams = {DbGridMetadata .GRID_UUID };
522+ String [] addParams = {
523+ DbGridMetadata .GRID_UUID
524+ };
514525 return basicInsertQuery (schemaName , tableName )
515526 + " "
516527 + writeOneLine (StringUtils .camelCaseToSnakeCase (headerElements ), addParams )
@@ -523,7 +534,10 @@ private String basicInsertQueryValuesGrid(
523534 */
524535 private String basicInsertQueryValuesITS (
525536 String schemaName , String tableName , String [] headerElements ) {
526- String [] addParams = {DbGridMetadata .GRID_UUID , TIME_SERIES };
537+ String [] addParams = {
538+ DbGridMetadata .GRID_UUID ,
539+ TIME_SERIES
540+ };
527541 return basicInsertQuery (schemaName , tableName )
528542 + " "
529543 + writeOneLine (StringUtils .camelCaseToSnakeCase (headerElements ), addParams )
@@ -545,7 +559,7 @@ private String writeOneLine(String[] entries, String[] addParams) {
545559
546560 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
547561
548- public void createClassTable (Class <? extends UniqueEntity > cls , String schemaName )
562+ public void createClassTable (Class <? extends Entity > cls , String schemaName )
549563 throws SQLException , ProcessorProviderException , EntityProcessorException {
550564 String query = queryCreateTableUniqueEntity (cls , schemaName );
551565 connector .executeUpdate (query );
0 commit comments