/*
 * Waltz - Enterprise Architecture
 * Copyright (C) 2016  Khartec Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.khartec.waltz.data.static_panel;

import com.khartec.waltz.model.staticpanel.ContentKind;
import com.khartec.waltz.model.staticpanel.ImmutableStaticPanel;
import com.khartec.waltz.model.staticpanel.StaticPanel;
import com.khartec.waltz.schema.tables.records.StaticPanelRecord;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.function.Function;

import static com.khartec.waltz.schema.tables.StaticPanel.STATIC_PANEL;

@Repository
public class StaticPanelDao {

    private final DSLContext dsl;


    private RecordMapper<Record, StaticPanel> panelMapper = r -> {
        StaticPanelRecord record = r.into(STATIC_PANEL);
        return ImmutableStaticPanel.builder()
                .id(record.getId())
                .content(record.getContent())
                .group(record.getGroup())
                .icon(record.getIcon())
                .encoding(ContentKind.valueOf(record.getEncoding()))
                .title(record.getTitle())
                .priority(record.getPriority())
                .width(record.getWidth())
                .build();
    };


    private static final Function<StaticPanel, StaticPanelRecord> TO_RECORD_MAPPER = panel -> {
        StaticPanelRecord record = new StaticPanelRecord();

        panel.id().ifPresent(id -> record.setId(id));

        record.setContent(panel.content());
        record.setTitle(panel.title());
        record.setWidth(panel.width());
        record.setPriority(panel.priority());
        record.setGroup(panel.group());
        record.setIcon(panel.icon());
        record.setEncoding(panel.encoding().name());

        return record;
    };


    @Autowired
    public StaticPanelDao(DSLContext dsl) {
        this.dsl = dsl;
    }


    public List<StaticPanel> findByGroups(String... groups) {
        return dsl
                .selectFrom(STATIC_PANEL)
                .where(STATIC_PANEL.GROUP.in(groups))
                .orderBy(STATIC_PANEL.PRIORITY.asc())
                .fetch(panelMapper);
    }


    public List<StaticPanel> findAll() {
        return dsl
                .selectFrom(STATIC_PANEL)
                .fetch(panelMapper);
    }


    public boolean update(StaticPanel panel) {
        StaticPanelRecord record = TO_RECORD_MAPPER.apply(panel);
        return dsl.executeUpdate(record) == 1;
    }


    public boolean create(StaticPanel panel) {
        StaticPanelRecord record = TO_RECORD_MAPPER.apply(panel);
        return dsl.executeInsert(record) == 1;
    }
}

@@ -1254,44 +1254,68 @@
 st;%0A
+%0A
 import 
-java.util.function.Function;%0A
+static com.khartec.waltz.common.Checks.checkNotNull;
 %0Aimp
@@ -2037,552 +2037,8 @@
 ;%0A%0A%0A
-    private static final Function%3CStaticPanel, StaticPanelRecord%3E TO_RECORD_MAPPER = panel -%3E %7B%0A        StaticPanelRecord record = new StaticPanelRecord();%0A%0A        panel.id().ifPresent(id -%3E record.setId(id));%0A%0A        record.setContent(panel.content());%0A        record.setTitle(panel.title());%0A        record.setWidth(panel.width());%0A        record.setPriority(panel.priority());%0A        record.setGroup(panel.group());%0A        record.setIcon(panel.icon());%0A        record.setEncoding(panel.encoding().name());%0A%0A        return record;%0A    %7D;%0A
 %0A%0A  
@@ -2090,24 +2090,73 @@
 text dsl) %7B%0A
+        checkNotNull(dsl, %22dsl cannot be null%22);%0A
         this
@@ -2680,38 +2680,24 @@
 ecord = 
-TO_RECORD_MAPPER.apply
+mkRecord
 (panel);
@@ -2716,32 +2716,22 @@
 urn 
-dsl.executeUpdate(record
+record.update(
 ) ==
@@ -2828,90 +2828,655 @@
 d = 
-TO_RECORD_MAPPER.apply(panel);%0A        return dsl.executeInsert(record) == 1
+mkRecord(panel);%0A        return record.insert() == 1;%0A    %7D%0A%0A    private StaticPanelRecord mkRecord(StaticPanel panel) %7B%0A        StaticPanelRecord record = dsl.newRecord(STATIC_PANEL);%0A%0A        panel.id().ifPresent(id -%3E %7B%0A            record.setId(id);%0A            record.changed(STATIC_PANEL.ID, false);%0A        %7D);%0A%0A        record.setContent(panel.content());%0A        record.setTitle(panel.title());%0A        record.setWidth(panel.width());%0A        record.setPriority(panel.priority());%0A        record.setGroup(panel.group());%0A        record.setIcon(panel.icon());%0A        record.setEncoding(panel.encoding().name());%0A%0A        return record
 ;%0A    %7D%0A
 %7D%0A
@@ -3473,10 +3473,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

add change initiatives to view history