|
| 1 | +// Copyright 2025 doubleSlash Net Business GmbH |
| 2 | +// |
| 3 | +// This file is part of KeepTime. |
| 4 | +// KeepTime is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package de.doubleslash.keeptime.view; |
| 18 | + |
| 19 | +import de.doubleslash.keeptime.model.ExternalProjectMapping; |
| 20 | +import de.doubleslash.keeptime.model.ExternalSystem; |
| 21 | +import de.doubleslash.keeptime.model.Model; |
| 22 | +import de.doubleslash.keeptime.model.Project; |
| 23 | +import de.doubleslash.keeptime.model.repos.ExternalProjectsMappingsRepository; |
| 24 | +import de.doubleslash.keeptime.model.settings.HeimatSettings; |
| 25 | +import de.doubleslash.keeptime.rest.integration.heimat.HeimatAPI; |
| 26 | +import de.doubleslash.keeptime.rest.integration.heimat.model.HeimatTask; |
| 27 | +import javafx.beans.property.SimpleObjectProperty; |
| 28 | +import javafx.beans.property.SimpleStringProperty; |
| 29 | +import javafx.collections.FXCollections; |
| 30 | +import javafx.collections.transformation.FilteredList; |
| 31 | +import javafx.fxml.FXML; |
| 32 | +import javafx.scene.control.*; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | +import org.springframework.stereotype.Component; |
| 36 | + |
| 37 | +import java.util.List; |
| 38 | +import java.util.Optional; |
| 39 | + |
| 40 | +@Component |
| 41 | +public class MapExternalProjectsController { |
| 42 | + |
| 43 | + private static final Logger LOG = LoggerFactory.getLogger(MapExternalProjectsController.class); |
| 44 | + |
| 45 | + private final Model model; |
| 46 | + private final HeimatSettings heimatSettings; |
| 47 | + private final ExternalProjectsMappingsRepository externalProjectsMappingsRepository; |
| 48 | + |
| 49 | + @FXML |
| 50 | + private TableView<ProjectMapping> mappingTableView; |
| 51 | + |
| 52 | + @FXML |
| 53 | + private Button saveButton; |
| 54 | + |
| 55 | + @FXML |
| 56 | + private Button cancelButton; |
| 57 | + |
| 58 | + @FXML |
| 59 | + private CheckBox filterOnlyWorkCheckBox; |
| 60 | + |
| 61 | + public MapExternalProjectsController(final Model model, HeimatSettings heimatSettings, |
| 62 | + ExternalProjectsMappingsRepository externalProjectsMappingsRepository) { |
| 63 | + this.model = model; |
| 64 | + this.heimatSettings = heimatSettings; |
| 65 | + this.externalProjectsMappingsRepository = externalProjectsMappingsRepository; |
| 66 | + } |
| 67 | + |
| 68 | + @FXML |
| 69 | + private void initialize() { |
| 70 | + |
| 71 | + final HeimatAPI heimatAPI = new HeimatAPI(heimatSettings.getHeimatUrl(), heimatSettings.getHeimatPat()); |
| 72 | + final List<HeimatTask> externalProjects = heimatAPI.getMyTasks(); |
| 73 | + |
| 74 | + final List<ExternalProjectMapping> alreadyMappedProjects = externalProjectsMappingsRepository.findByExternalSystemId( |
| 75 | + ExternalSystem.Heimat); |
| 76 | + |
| 77 | + final List<ProjectMapping> projectMappings = model.getSortedAvailableProjects().stream().map(p -> { |
| 78 | + final Optional<ExternalProjectMapping> mapping = alreadyMappedProjects.stream() |
| 79 | + .filter(mp -> mp.getProject().getId() |
| 80 | + == p.getId()) |
| 81 | + .findAny(); |
| 82 | + if (mapping.isEmpty()) { |
| 83 | + return new ProjectMapping(p, null); |
| 84 | + } |
| 85 | + final Optional<HeimatTask> any = externalProjects.stream() |
| 86 | + .filter(ep -> ep.id() == mapping.get().getExternalTaskId()) |
| 87 | + .findAny(); |
| 88 | + if (any.isEmpty()) { |
| 89 | + LOG.warn("A mapping exists but task does not exist anymore in HEIMAT! {}.", mapping.get()); |
| 90 | + return new ProjectMapping(p, null); |
| 91 | + } |
| 92 | + return new ProjectMapping(p, any.get()); |
| 93 | + }).toList(); |
| 94 | + |
| 95 | + final FilteredList<ProjectMapping> value = new FilteredList<>(FXCollections.observableArrayList(projectMappings)); |
| 96 | + filterOnlyWorkCheckBox.selectedProperty().addListener(((observable, oldValue, newValue) -> { |
| 97 | + if (Boolean.TRUE.equals(newValue)) |
| 98 | + value.setPredicate(pm -> pm.getProject().isWork()); |
| 99 | + else |
| 100 | + value.setPredicate(null); |
| 101 | + })); |
| 102 | + filterOnlyWorkCheckBox.setSelected(true); |
| 103 | + mappingTableView.setItems(value); |
| 104 | + |
| 105 | + // KeepTime Project column |
| 106 | + TableColumn<ProjectMapping, String> keepTimeColumn = new TableColumn<>("KeepTime Project"); |
| 107 | + keepTimeColumn.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().project.getName())); |
| 108 | + keepTimeColumn.setPrefWidth(200); |
| 109 | + |
| 110 | + // External Project column with dropdown |
| 111 | + TableColumn<ProjectMapping, HeimatTask> externalColumn = new TableColumn<>("Heimat Project"); |
| 112 | + externalColumn.setCellValueFactory(data -> new SimpleObjectProperty<>(data.getValue().heimatTask)); |
| 113 | + externalColumn.setCellFactory(col -> new TableCell<>() { |
| 114 | + // TODO search in box would be nice |
| 115 | + private final ComboBox<HeimatTask> comboBox = new ComboBox<>( |
| 116 | + FXCollections.observableArrayList(externalProjects)); |
| 117 | + |
| 118 | + @Override |
| 119 | + protected void updateItem(HeimatTask item, boolean empty) { |
| 120 | + super.updateItem(item, empty); |
| 121 | + // selected item |
| 122 | + comboBox.setButtonCell(new ListCell<>() { |
| 123 | + @Override |
| 124 | + protected void updateItem(HeimatTask item, boolean empty) { |
| 125 | + super.updateItem(item, empty); |
| 126 | + if (empty || item == null) { |
| 127 | + setText(null); |
| 128 | + } else { |
| 129 | + setText(item.projectName() + " - " + item.name()); |
| 130 | + } |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + // Dropdown |
| 135 | + comboBox.setCellFactory(param -> new ListCell<>() { |
| 136 | + @Override |
| 137 | + protected void updateItem(HeimatTask item, boolean empty) { |
| 138 | + super.updateItem(item, empty); |
| 139 | + if (item == null || empty) { |
| 140 | + setGraphic(null); |
| 141 | + setText(null); |
| 142 | + } else { |
| 143 | + setText(item.projectName() + " - " + item.name()); |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + if (empty) { |
| 149 | + setGraphic(null); |
| 150 | + setText(null); |
| 151 | + } else { |
| 152 | + comboBox.setValue(getTableView().getItems().get(getIndex()).getHeimatTask()); |
| 153 | + comboBox.setOnAction(e -> { |
| 154 | + ProjectMapping mapping = getTableView().getItems().get(getIndex()); |
| 155 | + mapping.setHeimatTask(comboBox.getValue()); |
| 156 | + }); |
| 157 | + setGraphic(comboBox); |
| 158 | + setText(null); |
| 159 | + } |
| 160 | + } |
| 161 | + }); |
| 162 | + externalColumn.setPrefWidth(400); |
| 163 | + |
| 164 | + mappingTableView.getColumns().addAll(keepTimeColumn, externalColumn); |
| 165 | + |
| 166 | + saveButton.setOnAction((ae) -> { |
| 167 | + LOG.debug("New mappings to be saved '{}'.", projectMappings); |
| 168 | + final List<ProjectMapping> newMappings = projectMappings.stream() |
| 169 | + .filter(pm -> pm.getHeimatTask() != null) |
| 170 | + .toList(); |
| 171 | + |
| 172 | + final List<ExternalProjectMapping> list = newMappings.stream().map(projectMapping -> { |
| 173 | + final Optional<ExternalProjectMapping> any = alreadyMappedProjects.stream() |
| 174 | + .filter(pm -> pm.getProject().getId() |
| 175 | + == projectMapping.project.getId()) |
| 176 | + .findAny(); |
| 177 | + final HeimatTask heimatTask = projectMapping.getHeimatTask(); |
| 178 | + if (any.isPresent()) { |
| 179 | + final ExternalProjectMapping projectMapping1 = any.get(); |
| 180 | + projectMapping1.setExternalProjectName(heimatTask.projectName()); |
| 181 | + projectMapping1.setExternalTaskId(heimatTask.id()); |
| 182 | + projectMapping1.setExternalTaskName(heimatTask.name()); |
| 183 | + projectMapping1.setExternalTaskMetadata(heimatTask.toString()); // TODO to json |
| 184 | + return projectMapping1; |
| 185 | + } |
| 186 | + return new ExternalProjectMapping(ExternalSystem.Heimat, heimatTask.projectName(), heimatTask.id(), |
| 187 | + heimatTask.name(), heimatTask.toString()// TODO to json |
| 188 | + , projectMapping.project); |
| 189 | + }).toList(); |
| 190 | + |
| 191 | + externalProjectsMappingsRepository.saveAll(list); |
| 192 | + // TODO remove mappings which were removed also from database |
| 193 | + }); |
| 194 | + |
| 195 | + cancelButton.setOnAction(ae -> { |
| 196 | + // TODO Close |
| 197 | + }); |
| 198 | + } |
| 199 | + |
| 200 | + public static class ProjectMapping { |
| 201 | + Project project; |
| 202 | + HeimatTask heimatTask; |
| 203 | + |
| 204 | + public ProjectMapping(final Project project, final HeimatTask heimatTask) { |
| 205 | + this.project = project; |
| 206 | + this.heimatTask = heimatTask; |
| 207 | + } |
| 208 | + |
| 209 | + public Project getProject() { |
| 210 | + return project; |
| 211 | + } |
| 212 | + |
| 213 | + public void setProject(final Project project) { |
| 214 | + this.project = project; |
| 215 | + } |
| 216 | + |
| 217 | + public HeimatTask getHeimatTask() { |
| 218 | + return heimatTask; |
| 219 | + } |
| 220 | + |
| 221 | + public void setHeimatTask(final HeimatTask heimatTask) { |
| 222 | + this.heimatTask = heimatTask; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | +} |
0 commit comments