Initial commit

This commit is contained in:
Paulo Gustavo Veiga
2020-11-07 11:56:38 -08:00
parent ad9cea069a
commit e4af8acdc2
27 changed files with 436 additions and 328 deletions

View File

@@ -1,10 +1,27 @@
/*
* Copyright [2015] [wisemapping]
*
* 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;
import com.wisemapping.model.User;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import java.util.List;
@@ -18,27 +35,34 @@ public class LabelManagerImpl extends HibernateDaoSupport
@Override
public void saveLabel(@NotNull final Label label) {
getSession().save(label);
currentSession().save(label);
}
@NotNull
@Override
@SuppressWarnings("unchecked")
public List<Label> getAllLabels(@NotNull final User user) {
return (List<Label>) getHibernateTemplate().find("from com.wisemapping.model.Label wisemapping where creator_id=?", user.getId());
var query = currentSession().createQuery("from com.wisemapping.model.Label wisemapping where creator_id=:creatorId");
query.setParameter("creatorId", user.getId());
return query.list();
}
@Nullable
@Override
public Label getLabelById(int id, @NotNull final User user) {
List<Label> labels = (List<Label>) getHibernateTemplate().find("from com.wisemapping.model.Label wisemapping where id=? and creator=?", new Object[]{id, user});
return getFirst(labels);
var query = currentSession().createQuery("from com.wisemapping.model.Label wisemapping where id=:id and creator=:creator");
query.setParameter("id", id);
query.setParameter("creator", user);
return getFirst(query.list());
}
@Nullable
@Override
public Label getLabelByTitle(@NotNull String title, @NotNull final User user) {
final List<Label> labels = (List<Label>) getHibernateTemplate().find("from com.wisemapping.model.Label wisemapping where title=? and creator=?", new Object[]{title, user});
return getFirst(labels);
var query = currentSession().createQuery("from com.wisemapping.model.Label wisemapping where title=:title and creator=:creator");
query.setParameter("title", title);
query.setParameter("creator", user);
return getFirst(query.list());
}
@Override

View File

@@ -1,28 +1,30 @@
/*
* Copyright [2015] [wisemapping]
*
* 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.
*/
* Copyright [2015] [wisemapping]
*
* 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.*;
import com.wisemapping.util.ZipUtils;
import org.hibernate.Query;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.hibernate.criterion.Junction;
@@ -40,7 +42,10 @@ public class MindmapManagerImpl
@Override
public Collaborator findCollaborator(@NotNull final String email) {
final Collaborator collaborator;
final List<Collaborator> collaborators = (List<Collaborator>) getHibernateTemplate().find("from com.wisemapping.model.Collaborator collaborator where email=?", email);
Query query = currentSession().createQuery("from com.wisemapping.model.Collaborator collaborator where email=:email");
query.setParameter("email", email);
final List<Collaborator> collaborators = query.list();
if (collaborators != null && !collaborators.isEmpty()) {
assert collaborators.size() == 1 : "More than one user with the same email!";
collaborator = collaborators.get(0);
@@ -57,7 +62,7 @@ public class MindmapManagerImpl
@Override
public List<MindMapHistory> getHistoryFrom(int mindmapId) {
final Criteria hibernateCriteria = getSession().createCriteria(MindMapHistory.class);
final Criteria hibernateCriteria = currentSession().createCriteria(MindMapHistory.class);
hibernateCriteria.add(Restrictions.eq("mindmapId", mindmapId));
hibernateCriteria.addOrder(Order.desc("creationTime"));
@@ -78,7 +83,7 @@ public class MindmapManagerImpl
@Override
public void purgeHistory(int mapId) throws IOException {
final Criteria hibernateCriteria = getSession().createCriteria(MindMapHistory.class);
final Criteria hibernateCriteria = currentSession().createCriteria(MindMapHistory.class);
hibernateCriteria.add(Restrictions.eq("mindmapId", mapId));
hibernateCriteria.addOrder(Order.desc("creationTime"));
@@ -92,17 +97,18 @@ public class MindmapManagerImpl
// If the map has not been modified in the last months, it means that I don't need to keep all the history ...
int max = mindmap.getLastModificationTime().before(yearAgo) ? 10 : 25;
final HibernateTemplate hibernateTemplate = getHibernateTemplate();
for (MindMapHistory history : historyList) {
byte[] zippedXml = history.getZippedXml();
if (new String(zippedXml).startsWith("<map")) {
history.setZippedXml(ZipUtils.bytesToZip(zippedXml));
getHibernateTemplate().update(history);
hibernateTemplate.update(history);
}
}
if (historyList.size() > max) {
for (int i = max; i < historyList.size(); i++) {
getHibernateTemplate().delete(historyList.get(i));
hibernateTemplate.delete(historyList.get(i));
}
}
}
@@ -110,7 +116,7 @@ public class MindmapManagerImpl
@Override
public List<Mindmap> search(MindMapCriteria criteria, int maxResult) {
final Criteria hibernateCriteria = getSession().createCriteria(Mindmap.class);
final Criteria hibernateCriteria = currentSession().createCriteria(Mindmap.class);
//always search public maps
hibernateCriteria.add(Restrictions.like("public", Boolean.TRUE));
@@ -154,19 +160,28 @@ public class MindmapManagerImpl
@Override
public List<Collaboration> findCollaboration(final long collaboratorId) {
return (List<Collaboration>) getHibernateTemplate().find("from com.wisemapping.model.Collaboration collaboration where colaborator_id=?", collaboratorId);
Query query = currentSession().createQuery("from com.wisemapping.model.Collaboration collaboration where colaboratorId=:colaboratorId");
query.setParameter("colaboratorId", collaboratorId);
return query.list();
}
@Override
public List<Collaboration> findCollaboration(final CollaborationRole collaborationRole) {
return (List<Collaboration>) getHibernateTemplate().find("from com.wisemapping.model.Collaboration collaboration where roleId=?", collaborationRole.ordinal());
Query query = currentSession().createQuery("from com.wisemapping.model.Collaboration collaboration where roleId=:roleId");
query.setParameter("roleId", collaborationRole.ordinal());
return query.list();
}
@Override
public Collaboration findCollaboration(final int mindmapId, final User user) {
final Collaboration result;
final List<Collaboration> mindMaps = (List<Collaboration>) getHibernateTemplate().find("from com.wisemapping.model.Collaboration collaboration where mindMap.id=? and colaborator_id=?", new Object[]{mindmapId, user.getId()});
Query query = currentSession().createQuery("from com.wisemapping.model.Collaboration collaboration where mindMap.id=:mindMapId and colaborator_id=:collaboratorId");
query.setParameter("mindMap", mindmapId);
query.setParameter("collaboratorId", user.getId());
final List<Collaboration> mindMaps = query.list();
if (mindMaps != null && !mindMaps.isEmpty()) {
result = mindMaps.get(0);
} else {
@@ -193,8 +208,9 @@ public class MindmapManagerImpl
}
@Override
@SuppressWarnings("unchecked")
public List<Mindmap> getAllMindmaps() {
return (List<Mindmap>) getHibernateTemplate().find("from com.wisemapping.model.Mindmap wisemapping");
return currentSession().createQuery("from com.wisemapping.model.Mindmap wisemapping").list();
}
@Override
@@ -206,7 +222,12 @@ public class MindmapManagerImpl
@Override
public Mindmap getMindmapByTitle(final String title, final User user) {
final Mindmap result;
List<Mindmap> mindMaps = (List<Mindmap>) getHibernateTemplate().find("from com.wisemapping.model.Mindmap wisemapping where title=? and creator=?", new Object[]{title, user});
Query query = currentSession().createQuery("from com.wisemapping.model.Mindmap wisemapping where title=:title and creator=:creator");
query.setParameter("title", title);
query.setParameter("creator", user);
List<Mindmap> mindMaps = query.list();
if (mindMaps != null && !mindMaps.isEmpty()) {
result = mindMaps.get(0);
} else {
@@ -223,7 +244,7 @@ public class MindmapManagerImpl
@Override
public void saveMindmap(Mindmap mindMap) {
assert mindMap != null : "Save Mindmap: Mindmap is required!";
getSession().save(mindMap);
currentSession().save(mindMap);
}
@Override
@@ -238,7 +259,7 @@ public class MindmapManagerImpl
@Override
public void removeMindmap(@NotNull final Mindmap mindMap) {
// Delete history first ...
final Criteria hibernateCriteria = getSession().createCriteria(MindMapHistory.class);
final Criteria hibernateCriteria = currentSession().createCriteria(MindMapHistory.class);
hibernateCriteria.add(Restrictions.eq("mindmapId", mindMap.getId()));
List list = hibernateCriteria.list();
getHibernateTemplate().deleteAll(list);
@@ -257,4 +278,3 @@ public class MindmapManagerImpl
getHibernateTemplate().saveOrUpdate(history);
}
}

View File

@@ -1,20 +1,20 @@
/*
* Copyright [2015] [wisemapping]
*
* 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.
*/
* Copyright [2015] [wisemapping]
*
* 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;
@@ -25,9 +25,8 @@ import com.wisemapping.model.AccessAuditory;
import org.hibernate.ObjectNotFoundException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.security.authentication.encoding.PasswordEncoder;
//import org.acegisecurity.providers.encoding.PasswordEncoder;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.List;
import java.util.Set;
@@ -42,29 +41,39 @@ public class UserManagerImpl
this.passwordEncoder = passwordEncoder;
}
@SuppressWarnings("unchecked")
public List<User> getAllUsers() {
return (List<User>) getHibernateTemplate().find("from com.wisemapping.model.User user");
return currentSession().createQuery("from com.wisemapping.model.User user").list();
}
@Override
public User getUserBy(@NotNull final String email) {
User user = null;
final List<User> users = (List<User>) getHibernateTemplate().find("from com.wisemapping.model.User colaborator where email=?", email);
var query = currentSession().createQuery("from com.wisemapping.model.User colaborator where email=:email");
query.setParameter("email", email);
final List<User> users = query.list();
if (users != null && !users.isEmpty()) {
assert users.size() == 1 : "More than one user with the same email!";
user = users.get(0);
}
return user;
}
@Override
public Collaborator getCollaboratorBy(final String email) {
final Collaborator cola;
final List cols = getHibernateTemplate().find("from com.wisemapping.model.Collaborator colaborator where email=?", email);
var query = currentSession().createQuery("from com.wisemapping.model.Collaborator colaborator where " +
"email=:email");
query.setParameter("email", email);
final List<User> cols = query.list();
if (cols != null && !cols.isEmpty()) {
assert cols.size() == 1 : "More than one colaborator with the same email!";
cola = (Collaborator) cols.get(0);
cola = cols.get(0);
} else {
cola = null;
}
@@ -74,9 +83,9 @@ public class UserManagerImpl
@Nullable
public User getUserBy(long id) {
User user = null;
try{
try {
user = getHibernateTemplate().get(User.class, id);
} catch (ObjectNotFoundException e){
} catch (ObjectNotFoundException e) {
// Ignore ...
}
return user;
@@ -85,13 +94,13 @@ public class UserManagerImpl
@Override
public void createUser(User user) {
assert user != null : "Trying to store a null user";
user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
user.setPassword(passwordEncoder.encode(user.getPassword()));
getHibernateTemplate().saveOrUpdate(user);
}
@Override
public User createUser(@NotNull User user, @NotNull Collaborator col) {
user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
user.setPassword(passwordEncoder.encode(user.getPassword()));
assert user != null : "Trying to store a null user";
final Set<Collaboration> set = col.getCollaborations();
@@ -121,14 +130,20 @@ public class UserManagerImpl
public void updateUser(@NotNull User user) {
assert user != null : "user is null";
user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
user.setPassword(passwordEncoder.encode(user.getPassword()));
getHibernateTemplate().update(user);
}
public User getUserByActivationCode(long code) {
final User user;
final List users = getHibernateTemplate().find("from com.wisemapping.model.User user where activationCode=?", code);
if (users != null && !users.isEmpty()) {
var query = currentSession().createQuery("from com.wisemapping.model.User user where " +
"activationCode=:activationCode");
query.setParameter("activationCode", code);
final List users = query.list();
if(users != null && !users.isEmpty()) {
assert users.size() == 1 : "More than one user with the same username!";
user = (User) users.get(0);
} else {