97 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;
import com.wisemapping.model.Label;
2014-01-26 18:21:01 -03:00
import com.wisemapping.model.User;
2023-10-30 05:34:45 +00:00
import jakarta.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.SelectionQuery;
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
2023-10-30 05:34:45 +00:00
private SessionFactory sessionFactory;
@Override
2014-01-26 18:18:49 -03:00
public void addLabel(@NotNull final Label label) {
saveLabel(label);
}
@Override
2014-01-26 18:18:49 -03:00
public void saveLabel(@NotNull final Label label) {
2023-10-30 05:34:45 +00:00
getSession().persist(label);
}
private Session getSession() {
return sessionFactory.getCurrentSession();
}
2014-01-26 18:21:01 -03:00
@NotNull
@Override
public List<Label> getAllLabels(@NotNull final User user) {
2023-10-30 05:34:45 +00:00
final SelectionQuery<Label> query = getSession().createSelectionQuery("from com.wisemapping.model.Label wisemapping where creator=:creatorId", Label.class);
query.setParameter("creatorId", user);
2020-11-07 11:56:38 -08:00
return query.list();
2014-01-26 18:21:01 -03:00
}
2014-01-28 02:28:16 -03:00
@Nullable
@Override
public Label getLabelById(int id, @NotNull final User user) {
2023-10-30 05:34:45 +00:00
final Session session = getSession();
final SelectionQuery<Label> query = session.createSelectionQuery("from com.wisemapping.model.Label wisemapping where id=:id and creator=:creator", Label.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
final List<Label> resultList = query.getResultList();
return getFirst(resultList);
2014-01-28 02:28:16 -03:00
}
2014-01-28 02:21:14 -03:00
@Nullable
@Override
public Label getLabelByTitle(@NotNull String title, @NotNull final User user) {
2023-10-30 05:34:45 +00:00
final SelectionQuery<Label> query = getSession().createSelectionQuery("from com.wisemapping.model.Label wisemapping where title=:title and creator=:creator", Label.class);
2020-11-07 11:56:38 -08:00
query.setParameter("title", title);
query.setParameter("creator", user);
return getFirst(query.list());
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
public void removeLabel(@NotNull Label label) {
2023-10-30 05:34:45 +00:00
getSession().remove(label);
2014-01-30 04:35:48 -03:00
}
2023-10-30 05:34:45 +00:00
@Nullable
private Label getFirst(final List<Label> labels) {
Label result = null;
if (labels != null && !labels.isEmpty()) {
result = labels.get(0);
}
return result;
}
2014-01-28 02:28:16 -03:00
}