2011-01-23 20:34:05 -03:00
/ *
* Copyright [ 2011 ] [ wisemapping ]
*
2011-01-23 21:03:12 -03:00
* Licensed under WiseMapping Public License , Version 1 . 0 ( the " License " ) .
* It is basically the Apache License , Version 2 . 0 ( the " License " ) plus the
2011-01-23 20:34:05 -03:00
* " 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 .
* /
2009-06-07 18:59:43 +00:00
package com.wisemapping.dao ;
import com.wisemapping.model.* ;
2012-06-06 00:48:46 -03:00
import org.jetbrains.annotations.NotNull ;
2009-06-07 18:59:43 +00:00
import org.springframework.orm.hibernate3.support.HibernateDaoSupport ;
import org.hibernate.criterion.Restrictions ;
import org.hibernate.criterion.SimpleExpression ;
import org.hibernate.criterion.Junction ;
import org.hibernate.criterion.Order ;
import org.hibernate.Criteria ;
import java.util.List ;
import java.util.Calendar ;
public class MindmapManagerImpl
extends HibernateDaoSupport
implements MindmapManager {
2012-06-09 15:49:19 -03:00
@Override
2012-06-17 23:21:02 -03:00
public Collaborator findCollaborator ( @NotNull final String email ) {
2012-02-21 14:22:43 -03:00
final Collaborator collaborator ;
2012-06-06 00:48:46 -03:00
final List < Collaborator > collaborators = getHibernateTemplate ( ) . find ( " from com.wisemapping.model.Collaborator collaborator where email=? " , email ) ;
if ( collaborators ! = null & & ! collaborators . isEmpty ( ) ) {
2012-07-15 02:04:53 -03:00
assert collaborators . size ( ) = = 1 : " More than one user with the same email! " ;
2012-06-06 00:48:46 -03:00
collaborator = collaborators . get ( 0 ) ;
2009-06-07 18:59:43 +00:00
} else {
2012-02-21 14:22:43 -03:00
collaborator = null ;
2009-06-07 18:59:43 +00:00
}
2012-02-21 14:22:43 -03:00
return collaborator ;
2009-06-07 18:59:43 +00:00
}
2012-06-09 15:49:19 -03:00
@Override
2009-06-07 18:59:43 +00:00
public List < MindMap > search ( MindMapCriteria criteria ) {
2012-06-06 00:48:46 -03:00
return search ( criteria , - 1 ) ;
2009-06-07 18:59:43 +00:00
}
2012-06-09 15:49:19 -03:00
@Override
2012-06-06 00:48:46 -03:00
public List < MindMapHistory > getHistoryFrom ( int mindmapId ) {
2009-06-07 18:59:43 +00:00
final Criteria hibernateCriteria = getSession ( ) . createCriteria ( MindMapHistory . class ) ;
2012-06-06 00:48:46 -03:00
hibernateCriteria . add ( Restrictions . eq ( " mindmapId " , mindmapId ) ) ;
hibernateCriteria . addOrder ( Order . desc ( " creationTime " ) ) ;
2009-06-07 18:59:43 +00:00
// Mientras no haya paginacion solo los 10 primeros
2011-03-28 15:58:01 +01:00
// This line throws errors in some environments, so getting all history and taking firsts 10 records
// hibernateCriteria.setMaxResults(10);
List list = hibernateCriteria . list ( ) ;
2012-06-06 00:48:46 -03:00
return list . subList ( 0 , ( 10 < list . size ( ) ? 10 : list . size ( ) ) ) ;
2009-06-07 18:59:43 +00:00
}
2012-06-09 15:49:19 -03:00
@Override
2012-06-06 00:48:46 -03:00
public MindMapHistory getHistory ( int historyId ) {
2012-06-09 15:49:19 -03:00
return getHibernateTemplate ( ) . get ( MindMapHistory . class , historyId ) ;
2009-06-07 18:59:43 +00:00
}
2012-06-17 23:21:02 -03:00
@Override
public void updateCollaboration ( @NotNull Collaboration collaboration ) {
getHibernateTemplate ( ) . save ( collaboration ) ;
}
2012-06-09 15:49:19 -03:00
@Override
2009-06-07 18:59:43 +00:00
public List < MindMap > search ( MindMapCriteria criteria , int maxResult ) {
final Criteria hibernateCriteria = getSession ( ) . createCriteria ( MindMap . class ) ;
//always search public maps
hibernateCriteria . add ( Restrictions . like ( " public " , Boolean . TRUE ) ) ;
if ( criteria ! = null ) {
final Junction junction ;
if ( criteria . isOrCriteria ( ) ) {
junction = Restrictions . disjunction ( ) ;
} else {
junction = Restrictions . conjunction ( ) ;
}
if ( criteria . getTitle ( ) ! = null & & criteria . getTitle ( ) . length ( ) > 0 ) {
final SimpleExpression titleRestriction = Restrictions . like ( " title " , " % " + criteria . getTitle ( ) + " % " ) ;
junction . add ( titleRestriction ) ;
}
if ( criteria . getDescription ( ) ! = null & & criteria . getDescription ( ) . length ( ) > 0 ) {
final SimpleExpression descriptionRestriction = Restrictions . like ( " description " , " % " + criteria . getDescription ( ) + " % " ) ;
junction . add ( descriptionRestriction ) ;
}
if ( criteria . getTags ( ) . size ( ) > 0 ) {
for ( String tag : criteria . getTags ( ) ) {
final SimpleExpression tagRestriction = Restrictions . like ( " tags " , " % " + tag + " % " ) ;
junction . add ( tagRestriction ) ;
}
}
hibernateCriteria . add ( junction ) ;
}
// if (maxResult>0)
// {
// hibernateCriteria.setMaxResults(maxResult);
// }
return hibernateCriteria . list ( ) ;
}
2012-06-09 15:49:19 -03:00
@Override
2012-06-17 21:43:34 -03:00
public Collaborator findCollaborator ( long id ) {
2012-06-09 15:49:19 -03:00
return getHibernateTemplate ( ) . get ( Collaborator . class , id ) ;
2009-06-07 18:59:43 +00:00
}
2012-06-09 15:49:19 -03:00
@Override
2012-06-17 21:43:34 -03:00
public List < Collaboration > findCollaboration ( final long collaboratorId ) {
return getHibernateTemplate ( ) . find ( " from com.wisemapping.model.Collaboration collaboration where colaborator_id=? " , collaboratorId ) ;
2009-06-07 18:59:43 +00:00
}
2012-06-09 15:49:19 -03:00
@Override
2012-06-17 21:43:34 -03:00
public List < Collaboration > findCollaboration ( final CollaborationRole collaborationRole ) {
return getHibernateTemplate ( ) . find ( " from com.wisemapping.model.Collaboration collaboration where roleId=? " , collaborationRole . ordinal ( ) ) ;
2009-06-07 18:59:43 +00:00
}
2012-06-09 22:49:54 -03:00
2012-06-09 15:49:19 -03:00
@Override
2012-06-17 21:43:34 -03:00
public Collaboration findCollaboration ( final int mindmapId , final User user ) {
2012-06-09 15:49:19 -03:00
final Collaboration result ;
2009-06-07 18:59:43 +00:00
2012-06-17 21:43:34 -03:00
final List < Collaboration > mindMaps = getHibernateTemplate ( ) . find ( " from com.wisemapping.model.Collaboration collaboration where mindMap.id=? and colaborator_id=? " , new Object [ ] { mindmapId , user . getId ( ) } ) ;
2009-06-07 18:59:43 +00:00
if ( mindMaps ! = null & & ! mindMaps . isEmpty ( ) ) {
result = mindMaps . get ( 0 ) ;
} else {
result = null ;
}
return result ;
}
2012-06-09 15:49:19 -03:00
@Override
2012-06-17 23:21:02 -03:00
public void addCollaborator ( @NotNull Collaborator collaborator ) {
2012-06-09 22:49:54 -03:00
assert collaborator ! = null : " ADD MINDMAP COLLABORATOR: Collaborator is required! " ;
2012-02-21 14:22:43 -03:00
getHibernateTemplate ( ) . save ( collaborator ) ;
2009-06-07 18:59:43 +00:00
}
2012-06-09 15:49:19 -03:00
@Override
2012-06-09 22:49:54 -03:00
public void removeCollaboration ( Collaboration collaboration ) {
2012-06-09 15:49:19 -03:00
getHibernateTemplate ( ) . delete ( collaboration ) ;
2009-06-07 18:59:43 +00:00
}
2012-06-09 15:49:19 -03:00
@Override
2012-06-09 22:49:54 -03:00
public void removeCollaborator ( @NotNull Collaborator collaborator ) {
2012-02-21 14:22:43 -03:00
getHibernateTemplate ( ) . delete ( collaborator ) ;
2009-06-07 18:59:43 +00:00
}
2012-06-09 15:49:19 -03:00
@Override
2009-06-07 18:59:43 +00:00
public List < MindMap > getAllMindmaps ( ) {
return getHibernateTemplate ( ) . find ( " from com.wisemapping.model.MindMap wisemapping " ) ;
}
2012-06-09 15:49:19 -03:00
@Override
2009-06-07 18:59:43 +00:00
public MindMap getMindmapById ( int mindmapId ) {
2012-03-14 01:49:05 -03:00
return getHibernateTemplate ( ) . get ( MindMap . class , mindmapId ) ;
2009-06-07 18:59:43 +00:00
}
2012-06-09 15:49:19 -03:00
@Override
2009-06-07 18:59:43 +00:00
public MindMap getMindmapByTitle ( final String title , final User user ) {
final MindMap result ;
2012-06-12 11:23:47 -03:00
List < MindMap > mindMaps = getHibernateTemplate ( ) . find ( " from com.wisemapping.model.MindMap wisemapping where title=? and creator=? " , new Object [ ] { title , user } ) ;
2009-06-07 18:59:43 +00:00
if ( mindMaps ! = null & & ! mindMaps . isEmpty ( ) ) {
result = mindMaps . get ( 0 ) ;
} else {
result = null ;
}
return result ;
}
2012-06-09 15:49:19 -03:00
@Override
2009-06-07 18:59:43 +00:00
public void addMindmap ( User user , MindMap mindMap ) {
saveMindmap ( mindMap ) ;
}
2012-06-09 15:49:19 -03:00
@Override
2009-06-07 18:59:43 +00:00
public void saveMindmap ( MindMap mindMap ) {
assert mindMap ! = null : " Save Mindmap: Mindmap is required! " ;
getSession ( ) . save ( mindMap ) ;
}
2012-06-09 15:49:19 -03:00
@Override
2012-06-06 00:48:46 -03:00
public void updateMindmap ( @NotNull MindMap mindMap , boolean saveHistory ) {
2009-06-07 18:59:43 +00:00
assert mindMap ! = null : " Save Mindmap: Mindmap is required! " ;
getHibernateTemplate ( ) . saveOrUpdate ( mindMap ) ;
2012-06-06 00:48:46 -03:00
if ( saveHistory ) {
2009-06-07 18:59:43 +00:00
saveHistory ( mindMap ) ;
}
}
2012-06-09 15:49:19 -03:00
@Override
2012-07-15 00:57:44 -03:00
public void removeMindmap ( @NotNull final MindMap mindMap ) {
2009-06-07 18:59:43 +00:00
getHibernateTemplate ( ) . delete ( mindMap ) ;
}
2012-07-15 00:57:44 -03:00
private void saveHistory ( @NotNull final MindMap mindMap ) {
2009-06-07 18:59:43 +00:00
final MindMapHistory history = new MindMapHistory ( ) ;
history . setXml ( mindMap . getXml ( ) ) ;
history . setCreationTime ( Calendar . getInstance ( ) ) ;
2012-07-15 00:57:44 -03:00
history . setEditor ( mindMap . getLastEditor ( ) ) ;
2009-06-07 18:59:43 +00:00
history . setMindmapId ( mindMap . getId ( ) ) ;
getHibernateTemplate ( ) . saveOrUpdate ( history ) ;
}
}