90 lines
3.2 KiB
Java
Raw Normal View History

2020-11-07 11:56:38 -08:00
/*
2022-03-17 18:47:34 -03:00
* Copyright [2022] [wisemapping]
2020-11-07 11:56:38 -08:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.wisemapping.dao;
2024-02-17 11:18:43 -08:00
import com.wisemapping.model.MindmapLabel;
import com.wisemapping.model.Account;
2024-01-13 18:49:49 -08:00
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import org.jetbrains.annotations.NotNull;
2014-01-28 02:28:16 -03:00
import org.jetbrains.annotations.Nullable;
2023-12-04 20:00:06 -08:00
import org.springframework.beans.factory.annotation.Autowired;
2023-10-30 05:34:45 +00:00
import org.springframework.stereotype.Repository;
2014-01-26 18:21:01 -03:00
import java.util.List;
2023-11-19 08:05:38 -08:00
@Repository("labelManager")
2023-10-30 05:34:45 +00:00
public class LabelManagerImpl
implements LabelManager {
2023-12-04 20:00:06 -08:00
@Autowired
2024-01-13 18:49:49 -08:00
private EntityManager entityManager;
@Override
2024-02-17 11:18:43 -08:00
public void addLabel(@NotNull final MindmapLabel label) {
saveLabel(label);
}
@Override
2024-02-17 11:18:43 -08:00
public void saveLabel(@NotNull final MindmapLabel label) {
2024-01-13 18:49:49 -08:00
entityManager.persist(label);
}
2014-01-26 18:21:01 -03:00
@NotNull
@Override
2024-02-17 11:18:43 -08:00
public List<MindmapLabel> getAllLabels(@NotNull final Account user) {
final TypedQuery<MindmapLabel> query = entityManager.createQuery("from com.wisemapping.model.MindmapLabel wisemapping where creator=:creatorId", MindmapLabel.class);
2023-10-30 05:34:45 +00:00
query.setParameter("creatorId", user);
2024-01-13 18:49:49 -08:00
return query.getResultList();
2014-01-26 18:21:01 -03:00
}
2014-01-28 02:28:16 -03:00
@Nullable
@Override
2024-02-17 11:18:43 -08:00
public MindmapLabel getLabelById(int id, @NotNull final Account user) {
final TypedQuery<MindmapLabel> query = entityManager.createQuery("from com.wisemapping.model.MindmapLabel wisemapping where id=:id and creator=:creator", MindmapLabel.class);
2020-11-07 11:56:38 -08:00
query.setParameter("id", id);
query.setParameter("creator", user);
2023-10-30 05:34:45 +00:00
2024-02-17 11:18:43 -08:00
final List<MindmapLabel> resultList = query.getResultList();
2023-10-30 05:34:45 +00:00
return getFirst(resultList);
2014-01-28 02:28:16 -03:00
}
2014-01-28 02:21:14 -03:00
@Nullable
@Override
2024-02-17 11:18:43 -08:00
public MindmapLabel getLabelByTitle(@NotNull String title, @NotNull final Account user) {
final TypedQuery<MindmapLabel> query = entityManager.createQuery("from com.wisemapping.model.MindmapLabel wisemapping where title=:title and creator=:creator", MindmapLabel.class);
2020-11-07 11:56:38 -08:00
query.setParameter("title", title);
query.setParameter("creator", user);
2024-01-15 18:09:01 -08:00
return query.getResultList().stream().findFirst().orElse(null);
2014-01-28 02:21:14 -03:00
}
2014-01-28 02:28:16 -03:00
2014-01-30 04:35:48 -03:00
@Override
2024-02-17 11:18:43 -08:00
public void removeLabel(@NotNull MindmapLabel label) {
2024-01-13 18:49:49 -08:00
entityManager.remove(label);
2014-01-30 04:35:48 -03:00
}
2023-10-30 05:34:45 +00:00
@Nullable
2024-02-17 11:18:43 -08:00
private MindmapLabel getFirst(final List<MindmapLabel> labels) {
MindmapLabel result = null;
if (labels != null && !labels.isEmpty()) {
result = labels.get(0);
}
return result;
}
2014-01-28 02:28:16 -03:00
}