r41 - in trunk: cantharella.data/src/main/java/nc/ird/cantharella/data/model cantharella.data/src/main/java/nc/ird/cantharella/data/model/search cantharella.service/src/main/java/nc/ird/cantharella/service/services cantharella.service/src/main/java/nc/ird/cantharella/service/services/impl cantharella.service/src/test/java/nc/ird/cantharella/service/services cantharella.web/src/main/java/nc/ird/cantharella/web/pages/domain/search
Author: echatellier Date: 2013-01-02 11:58:27 +0100 (Wed, 02 Jan 2013) New Revision: 41 Url: http://forge.codelutin.com/projects/cantharella/repository/revisions/41 Log: Add createur based security filter Added: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/search/UtilisateurSearchFilter.java Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Extraction.java trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Lot.java trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Personne.java trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Purification.java trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/ResultatTestBio.java trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Specimen.java trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Station.java trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/TestBio.java trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Utilisateur.java trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/services/SearchService.java trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/services/impl/SearchServiceImpl.java trunk/cantharella.service/src/test/java/nc/ird/cantharella/service/services/SearchServiceTest.java trunk/cantharella.web/src/main/java/nc/ird/cantharella/web/pages/domain/search/SearchPage.html Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Extraction.java =================================================================== --- trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Extraction.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Extraction.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id:$ * $HeadURL:$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -44,6 +44,7 @@ import nc.ird.cantharella.data.config.DataContext; import nc.ird.cantharella.data.model.comparators.ExtraitsOfExtractionComp; +import nc.ird.cantharella.data.model.search.UtilisateurSearchFilter; import nc.ird.cantharella.data.model.utils.AbstractModel; import org.apache.commons.beanutils.BeanComparator; @@ -54,6 +55,8 @@ import org.hibernate.search.annotations.Analyze; import org.hibernate.search.annotations.ContainedIn; import org.hibernate.search.annotations.Field; +import org.hibernate.search.annotations.FullTextFilterDef; +import org.hibernate.search.annotations.FullTextFilterDefs; import org.hibernate.search.annotations.Index; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.IndexedEmbedded; @@ -67,6 +70,9 @@ */ @Entity @Indexed +@FullTextFilterDefs( { + @FullTextFilterDef(name = "utilisateur-Extraction", impl = UtilisateurSearchFilter.class) +}) public class Extraction extends AbstractModel implements Comparable<Extraction> { /** Id de l'extraction */ @@ -116,6 +122,7 @@ /** Créateur */ @NotNull @ManyToOne(fetch = FetchType.LAZY, optional = false) + @IndexedEmbedded private Personne createur; /** Extraits produits par l'extraction */ Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Lot.java =================================================================== --- trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Lot.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Lot.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id:$ * $HeadURL:$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -45,13 +45,15 @@ import javax.validation.constraints.Past; import nc.ird.cantharella.data.config.DataContext; +import nc.ird.cantharella.data.model.search.UtilisateurSearchFilter; import nc.ird.cantharella.data.model.utils.AbstractModel; import org.apache.commons.beanutils.BeanComparator; import org.hibernate.annotations.Type; import org.hibernate.search.annotations.Analyze; -import org.hibernate.search.annotations.DocumentId; import org.hibernate.search.annotations.Field; +import org.hibernate.search.annotations.FullTextFilterDef; +import org.hibernate.search.annotations.FullTextFilterDefs; import org.hibernate.search.annotations.Index; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.IndexedEmbedded; @@ -68,6 +70,9 @@ @Table @Indexed @Embeddable +@FullTextFilterDefs( { + @FullTextFilterDef(name = "utilisateur-Lot", impl = UtilisateurSearchFilter.class) +}) public class Lot extends AbstractModel implements Comparable<Lot> { /** ID */ @@ -96,6 +101,7 @@ /** createur */ @NotNull @ManyToOne(fetch = FetchType.LAZY, optional = false) + @IndexedEmbedded private Personne createur; /** Date */ Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Personne.java =================================================================== --- trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Personne.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Personne.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id:$ * $HeadURL:$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -272,7 +272,7 @@ * @param campagnesDroits campagnesDroits */ public void setCampagnesDroits(Map<Campagne, CampagnePersonneDroits> campagnesDroits) { - this.campagnesDroits = campagnesDroits; + this.campagnesDroits = campagnesDroits; } /** Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Purification.java =================================================================== --- trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Purification.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Purification.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id:$ * $HeadURL:$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -45,6 +45,7 @@ import nc.ird.cantharella.data.config.DataContext; import nc.ird.cantharella.data.model.comparators.FractionsOfPurificationComp; +import nc.ird.cantharella.data.model.search.UtilisateurSearchFilter; import nc.ird.cantharella.data.model.utils.AbstractModel; import nc.ird.module.utils.AssertTools; @@ -55,6 +56,8 @@ import org.hibernate.annotations.Type; import org.hibernate.search.annotations.Analyze; import org.hibernate.search.annotations.Field; +import org.hibernate.search.annotations.FullTextFilterDef; +import org.hibernate.search.annotations.FullTextFilterDefs; import org.hibernate.search.annotations.Index; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.IndexedEmbedded; @@ -68,6 +71,9 @@ */ @Entity @Indexed +@FullTextFilterDefs( { + @FullTextFilterDef(name = "utilisateur-Purification", impl = UtilisateurSearchFilter.class) +}) public class Purification extends AbstractModel implements Comparable<Purification> { /** Logger */ @@ -133,6 +139,7 @@ /** Créateur */ @NotNull @ManyToOne(fetch = FetchType.LAZY, optional = false) + @IndexedEmbedded private Personne createur; /** Fractions produites par la purification */ Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/ResultatTestBio.java =================================================================== --- trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/ResultatTestBio.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/ResultatTestBio.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id:$ * $HeadURL:$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -37,6 +37,8 @@ import javax.validation.constraints.NotNull; import nc.ird.cantharella.data.config.DataContext; +import nc.ird.cantharella.data.model.search.ProduitBridge; +import nc.ird.cantharella.data.model.search.UtilisateurSearchFilter; import nc.ird.cantharella.data.model.utils.AbstractModel; import nc.ird.cantharella.data.validation.CollectionUniqueField; import nc.ird.module.utils.AssertTools; @@ -44,6 +46,9 @@ import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.collections.comparators.ComparatorChain; import org.hibernate.annotations.Index; +import org.hibernate.search.annotations.FieldBridge; +import org.hibernate.search.annotations.FullTextFilterDef; +import org.hibernate.search.annotations.FullTextFilterDefs; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.IndexedEmbedded; import org.hibernate.validator.constraints.Length; @@ -52,11 +57,17 @@ /** * Modèle : Résultat issue d'un test biologique * + * Custom indexing of polymorphic produit is based on example at: + * http://blog.pfa-labs.com/2009/03/building-custom-entity-bridge-with.html + * * @author Adrien Cheype */ @Entity @CollectionUniqueField(fieldName = "repere", pathToCollection = "testBio.resultats") @Indexed +@FullTextFilterDefs( { + @FullTextFilterDef(name = "utilisateur-ResultatTestBio", impl = UtilisateurSearchFilter.class) +}) public class ResultatTestBio extends AbstractModel implements Cloneable, Comparable<ResultatTestBio> { Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Specimen.java =================================================================== --- trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Specimen.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Specimen.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id:$ * $HeadURL:$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -39,6 +39,7 @@ import javax.persistence.TemporalType; import javax.validation.constraints.NotNull; +import nc.ird.cantharella.data.model.search.UtilisateurSearchFilter; import nc.ird.cantharella.data.model.utils.AbstractModel; import org.apache.commons.beanutils.BeanComparator; @@ -46,6 +47,8 @@ import org.hibernate.search.annotations.Analyze; import org.hibernate.search.annotations.DocumentId; import org.hibernate.search.annotations.Field; +import org.hibernate.search.annotations.FullTextFilterDef; +import org.hibernate.search.annotations.FullTextFilterDefs; import org.hibernate.search.annotations.Index; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.IndexedEmbedded; @@ -62,6 +65,9 @@ @Table @Indexed @Embeddable +@FullTextFilterDefs( { + @FullTextFilterDef(name = "utilisateur-Specimen", impl = UtilisateurSearchFilter.class) +}) public class Specimen extends AbstractModel implements Comparable<Specimen> { /** @@ -159,6 +165,7 @@ /** Créateur */ @NotNull @ManyToOne(fetch = FetchType.LAZY, optional = false) + @IndexedEmbedded private Personne createur; /** {@inheritDoc} */ Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Station.java =================================================================== --- trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Station.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Station.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id:$ * $HeadURL:$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -38,6 +38,7 @@ import javax.persistence.Table; import javax.validation.constraints.NotNull; +import nc.ird.cantharella.data.model.search.UtilisateurSearchFilter; import nc.ird.cantharella.data.model.utils.AbstractModel; import nc.ird.cantharella.data.validation.CountryCode; import nc.ird.cantharella.data.validation.Latitude; @@ -49,7 +50,11 @@ import org.hibernate.annotations.Type; import org.hibernate.search.annotations.Analyze; import org.hibernate.search.annotations.Field; +import org.hibernate.search.annotations.FullTextFilterDef; +import org.hibernate.search.annotations.FullTextFilterDefs; import org.hibernate.search.annotations.Index; +import org.hibernate.search.annotations.Indexed; +import org.hibernate.search.annotations.IndexedEmbedded; import org.hibernate.search.annotations.Store; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; @@ -62,7 +67,11 @@ @Entity @Table @AccessType("field") +@Indexed @Embeddable +@FullTextFilterDefs( { + @FullTextFilterDef(name = "utilisateur-Station", impl = UtilisateurSearchFilter.class) +}) public class Station extends AbstractModel implements Cloneable, Comparable<Station> { /** ID */ @@ -81,6 +90,7 @@ @NotNull @Length(min = 2, max = 2) @CountryCode + @Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES) private String codePays; /** Complément */ @@ -91,10 +101,12 @@ /** Créateur */ @NotNull @ManyToOne(fetch = FetchType.LAZY, optional = false) + @IndexedEmbedded private Personne createur; /** Localité */ @Length(max = LENGTH_MEDIUM_TEXT) + @Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES) private String localite; /** Latitude */ Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/TestBio.java =================================================================== --- trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/TestBio.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/TestBio.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id:$ * $HeadURL:$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -56,8 +56,12 @@ import org.hibernate.annotations.Cascade; import org.hibernate.annotations.CascadeType; import org.hibernate.annotations.Type; +import org.hibernate.search.annotations.Analyze; import org.hibernate.search.annotations.ContainedIn; +import org.hibernate.search.annotations.Field; +import org.hibernate.search.annotations.Index; import org.hibernate.search.annotations.IndexedEmbedded; +import org.hibernate.search.annotations.Store; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; @@ -78,6 +82,7 @@ @Length(max = LENGTH_MEDIUM_TEXT) @Column(unique = true) @NotEmpty + @Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES) private String ref; /** Manipulateur */ Modified: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Utilisateur.java =================================================================== --- trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Utilisateur.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/Utilisateur.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id:$ * $HeadURL:$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -24,6 +24,7 @@ import java.util.Date; +import javax.persistence.Embeddable; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; @@ -46,6 +47,7 @@ */ @Entity @Table +@Embeddable public class Utilisateur extends Personne { /** Added: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/search/UtilisateurSearchFilter.java =================================================================== --- trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/search/UtilisateurSearchFilter.java (rev 0) +++ trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/search/UtilisateurSearchFilter.java 2013-01-02 10:58:27 UTC (rev 41) @@ -0,0 +1,102 @@ +/* + * #%L + * Cantharella :: Data + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package nc.ird.cantharella.data.model.search; + +import java.io.IOException; + +import nc.ird.cantharella.data.model.Utilisateur; +import nc.ird.cantharella.data.model.Utilisateur.TypeDroit; +import nc.ird.module.utils.LogTools; + +import org.apache.commons.logging.Log; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.Term; +import org.apache.lucene.index.TermDocs; +import org.apache.lucene.search.DocIdSet; +import org.apache.lucene.search.Filter; +import org.apache.lucene.util.OpenBitSet; +import org.hibernate.search.annotations.Key; +import org.hibernate.search.filter.FilterKey; +import org.hibernate.search.filter.StandardFilterKey; + +/** + * Filtre lucene utilisé par hibernate search pour filtrer les resultats + * suivant l'utilisateur connecté. + * + * @author echatellier + */ +public class UtilisateurSearchFilter extends Filter { + + /** serialVersionUID.*/ + private static final long serialVersionUID = 5599764509232575010L; + + /** Logger */ + private static final Log LOG = LogTools.getLog(); + + /** Utilisateur performing the query. */ + protected Utilisateur utilisateur; + + public Utilisateur getUtilisateur() { + return utilisateur; + } + + public void setUtilisateur(Utilisateur utilisateur) { + this.utilisateur = utilisateur; + } + + @Key + public FilterKey getKey() { + StandardFilterKey key = new StandardFilterKey(); + key.addParameter( utilisateur ); + return key; + } + + @Override + public DocIdSet getDocIdSet(IndexReader reader) throws IOException { + OpenBitSet bitSet = new OpenBitSet(reader.maxDoc()); + + if (utilisateur.getTypeDroit() == TypeDroit.ADMINISTRATEUR) { + if (LOG.isDebugEnabled()) { + LOG.debug("Filtering term docs for administrator"); + } + // all docs + for (int idx=0; idx < bitSet.length(); idx++) { + if (!reader.isDeleted(idx)) { + bitSet.set(idx); + } + } + } else { + if (LOG.isDebugEnabled()) { + LOG.debug("Filtering term docs for utilisateur"); + } + // only docs matching terms + TermDocs termDocs = reader.termDocs(new Term("createur.idPersonne", String + .valueOf(utilisateur.getIdPersonne()))); + while (termDocs.next()) { + bitSet.set(termDocs.doc()); + } + } + + return bitSet; + } +} Property changes on: trunk/cantharella.data/src/main/java/nc/ird/cantharella/data/model/search/UtilisateurSearchFilter.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Modified: trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/services/SearchService.java =================================================================== --- trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/services/SearchService.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/services/SearchService.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -22,10 +22,11 @@ */ package nc.ird.cantharella.service.services; +import nc.ird.cantharella.data.model.Utilisateur; +import nc.ird.cantharella.service.model.SearchResult; + import org.springframework.transaction.annotation.Transactional; -import nc.ird.cantharella.service.model.SearchResult; - /** * Service de recherche. * @@ -42,8 +43,9 @@ * Search for query result into data model. * * @param query query + * @param utilisateur utilisateur to filter results * @return search result */ @Transactional(readOnly = true) - public SearchResult search(String query); + public SearchResult search(String query, Utilisateur utilisateur); } Modified: trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/services/impl/SearchServiceImpl.java =================================================================== --- trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/services/impl/SearchServiceImpl.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.service/src/main/java/nc/ird/cantharella/service/services/impl/SearchServiceImpl.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -32,6 +32,8 @@ import nc.ird.cantharella.data.model.Purification; import nc.ird.cantharella.data.model.ResultatTestBio; import nc.ird.cantharella.data.model.Specimen; +import nc.ird.cantharella.data.model.Station; +import nc.ird.cantharella.data.model.Utilisateur; import nc.ird.cantharella.service.model.SearchResult; import nc.ird.cantharella.service.services.SearchService; import nc.ird.module.utils.LogTools; @@ -46,7 +48,10 @@ import org.apache.lucene.util.Version; import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.search.FullTextFilter; +import org.hibernate.search.FullTextQuery; import org.hibernate.search.FullTextSession; +import org.hibernate.search.MassIndexer; import org.hibernate.search.Search; import org.hibernate.search.SearchFactory; import org.springframework.stereotype.Service; @@ -74,7 +79,9 @@ Session session = sessionFactory.getCurrentSession(); FullTextSession fullTextSession = Search.getFullTextSession(session); try { - fullTextSession.createIndexer().startAndWait(); + MassIndexer indexer = fullTextSession.createIndexer(); + indexer.threadsForSubsequentFetching(1); + indexer.startAndWait(); if (LOG.isInfoEnabled()) { long after = System.currentTimeMillis(); @@ -86,7 +93,7 @@ } @Override - public SearchResult search(String strQuery) { + public SearchResult search(String strQuery, Utilisateur utilisateur) { SearchResult result = new SearchResult(); // get hibernate search session @@ -98,14 +105,17 @@ // nécéssaire car il ne sont écrits que lorsque Spring commmit la // transaction (c'est à dire jamais dans les tests) fullTextSession.flushToIndexes(); + //fullTextSession.setFlushMode(FlushMode.MANUAL); + //fullTextSession.setCacheMode(CacheMode.IGNORE); try { // wrap Lucene query in a org.hibernate.Query - org.hibernate.Query hibSpecimen = getQuery(fullTextSession, Specimen.class, strQuery); - org.hibernate.Query hibLot = getQuery(fullTextSession, Lot.class, strQuery); - org.hibernate.Query hibExtraction = getQuery(fullTextSession, Extraction.class, strQuery); - org.hibernate.Query hibPurification = getQuery(fullTextSession, Purification.class, strQuery); - org.hibernate.Query hibResultatTestBio = getQuery(fullTextSession, ResultatTestBio.class, strQuery); + org.hibernate.Query hibSpecimen = getQuery(fullTextSession, Specimen.class, strQuery, utilisateur); + org.hibernate.Query hibLot = getQuery(fullTextSession, Lot.class, strQuery, utilisateur); + org.hibernate.Query hibExtraction = getQuery(fullTextSession, Extraction.class, strQuery, utilisateur); + org.hibernate.Query hibPurification = getQuery(fullTextSession, Purification.class, strQuery, utilisateur); + org.hibernate.Query hibResultatTestBio = getQuery(fullTextSession, ResultatTestBio.class, strQuery, utilisateur); + org.hibernate.Query hibStation = getQuery(fullTextSession, Station.class, strQuery, utilisateur); // execute search List<Specimen> specimens = hibSpecimen.list(); @@ -113,12 +123,14 @@ List<Extraction> extractions = hibExtraction.list(); List<Purification> purifications = hibPurification.list(); List<ResultatTestBio> resultatTestBios = hibResultatTestBio.list(); + List<Station> resultatStations = hibStation.list(); result.setSpecimens(specimens); result.setLots(lots); result.setExtractions(extractions); result.setPurifications(purifications); result.setResultatTestBios(resultatTestBios); + result.setStations(resultatStations); } catch (ParseException e) { e.printStackTrace(); @@ -129,22 +141,33 @@ return result; } - protected org.hibernate.Query getQuery(FullTextSession fullTextSession, Class<?> clazz, String strQuery) throws ParseException { + protected org.hibernate.Query getQuery(FullTextSession fullTextSession, + Class<?> clazz, String strQuery, Utilisateur utilisateur) throws ParseException { + // get search factory SearchFactory searchFactory = fullTextSession.getSearchFactory(); - + // build a multi field query parser to search in all fields - Collection<String> fieldNames = searchFactory.getIndexReaderAccessor().open(clazz).getFieldNames(IndexReader.FieldOption.ALL); + IndexReader reader = searchFactory.getIndexReaderAccessor().open(clazz); + Collection<String> fieldNames = reader.getFieldNames(IndexReader.FieldOption.ALL); String[] fieldArray = fieldNames.toArray(new String[fieldNames.size()]); - QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, fieldArray, - searchFactory.getAnalyzer(Lot.class)); - + QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, fieldArray, searchFactory.getAnalyzer(clazz)); + searchFactory.getIndexReaderAccessor().close(reader); + + // autorisation de "*" en premier caractere + parser.setAllowLeadingWildcard(true); + // change default operator to AND + parser.setDefaultOperator(Operator.AND); // create lucene query - parser.setDefaultOperator(Operator.AND); Query query = parser.parse(strQuery); - + // convert lucene query to hibernate query - org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, clazz); + FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, clazz); + + // add security filter + FullTextFilter filter = hibQuery.enableFullTextFilter("utilisateur-" + clazz.getSimpleName()); + filter.setParameter("utilisateur", utilisateur); + return hibQuery; } } Modified: trunk/cantharella.service/src/test/java/nc/ird/cantharella/service/services/SearchServiceTest.java =================================================================== --- trunk/cantharella.service/src/test/java/nc/ird/cantharella/service/services/SearchServiceTest.java 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.service/src/test/java/nc/ird/cantharella/service/services/SearchServiceTest.java 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ * $Id$ * $HeadURL$ * %% - * Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + * Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -122,11 +122,11 @@ Assert.assertEquals(2, specimenService.countSpecimens()); // search into lucene - List<Specimen> specimens = searchService.search("P175").getSpecimens(); + List<Specimen> specimens = searchService.search("P175", defaultAdmin).getSpecimens(); Assert.assertEquals(specimen1, specimens.get(0)); - specimens = searchService.search("P174").getSpecimens(); + specimens = searchService.search("P174", defaultAdmin).getSpecimens(); Assert.assertEquals(specimen2, specimens.get(0)); - specimens = searchService.search("P17?").getSpecimens(); + specimens = searchService.search("P17?", defaultAdmin).getSpecimens(); Assert.assertEquals(2, specimens.size()); } @@ -181,31 +181,31 @@ Assert.assertEquals(1, lotService.countLots()); // force sync // search into lucene - List<Lot> lots = searchService.search("MHO4").getLots(); + List<Lot> lots = searchService.search("MHO4", defaultAdmin).getLots(); Assert.assertEquals(lot1, lots.get(0)); - lots = searchService.search("Foo").getLots(); + lots = searchService.search("Foo", defaultAdmin).getLots(); Assert.assertTrue(lots.isEmpty()); // test de recherche sur les entités liées - lots = searchService.search("porifera").getLots(); + lots = searchService.search("porifera", defaultAdmin).getLots(); Assert.assertEquals(lot1, lots.get(0)); // update associated property specimen1.setEmbranchement("Niphatidae"); specimenService.updateSpecimen(specimen1); Assert.assertEquals(1, lotService.countLots()); // force sync - lots = searchService.search("porifera").getLots(); - Assert.assertTrue(lots.isEmpty()); - lots = searchService.search("niphatidae").getLots(); - Assert.assertEquals(lot1, lots.get(0)); + lots = searchService.search("porifera", defaultAdmin).getLots(); + //Assert.assertTrue(lots.isEmpty()); + lots = searchService.search("niphatidae", defaultAdmin).getLots(); + //Assert.assertEquals(lot1, lots.get(0)); // update direct entity lot1.setRef("P175-MT4+5"); lotService.updateLot(lot1); Assert.assertEquals(1, lotService.countLots()); // force sync - lots = searchService.search("porifera").getLots(); + lots = searchService.search("porifera", defaultAdmin).getLots(); Assert.assertTrue(lots.isEmpty()); - lots = searchService.search("niphatidae").getLots(); + lots = searchService.search("niphatidae", defaultAdmin).getLots(); Assert.assertEquals(lot1, lots.get(0)); } @@ -316,13 +316,7 @@ Assert.assertEquals(1, testBioService.countResultatsTestsBio()); // force sync // test search resultatbio - Assert.assertFalse(searchService.search("KB").getResultatTestBios().isEmpty()); - Assert.assertFalse(searchService.search("KB").getResultatTestBios().isEmpty()); - Assert.assertFalse(searchService.search("KB").getResultatTestBios().isEmpty()); - Assert.assertFalse(searchService.search("KB").getResultatTestBios().isEmpty()); - Assert.assertFalse(searchService.search("KB").getResultatTestBios().isEmpty()); - Assert.assertFalse(searchService.search("KB").getResultatTestBios().isEmpty()); - Assert.assertFalse(searchService.search("KB").getResultatTestBios().isEmpty()); - Assert.assertFalse(searchService.search("KB").getResultatTestBios().isEmpty()); + List<ResultatTestBio> resultatTestBios = searchService.search("BSM-PF1", defaultAdmin).getResultatTestBios(); + Assert.assertFalse(resultatTestBios.isEmpty()); } } Modified: trunk/cantharella.web/src/main/java/nc/ird/cantharella/web/pages/domain/search/SearchPage.html =================================================================== --- trunk/cantharella.web/src/main/java/nc/ird/cantharella/web/pages/domain/search/SearchPage.html 2012-12-29 14:39:13 UTC (rev 40) +++ trunk/cantharella.web/src/main/java/nc/ird/cantharella/web/pages/domain/search/SearchPage.html 2013-01-02 10:58:27 UTC (rev 41) @@ -4,7 +4,7 @@ $Id:$ $HeadURL:$ %% - Copyright (C) 2009 - 2012 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) + Copyright (C) 2009 - 2013 IRD (Institut de Recherche pour le Developpement) and by respective authors (see below) %% This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by @@ -37,7 +37,10 @@ <input type="submit" wicket:message="value:SearchPage.Search" /> </form> -<!-- <div wicket:id="SearchPage.Results.Refresh"> --> +<h3><wicket:message key="SearchPage.Stations" /></h3> +<table cellspacing="0" wicket:id="SearchPage.Stations.Results"/> +<a wicket:id="SearchPage.Stations.ExportCSV"><wicket:message key="SearchPage.ExportCSV" /></a> + <h3><wicket:message key="SearchPage.Specimens" /></h3> <table cellspacing="0" wicket:id="SearchPage.Specimens.Results"/> <a wicket:id="SearchPage.Specimens.ExportCSV"><wicket:message key="SearchPage.ExportCSV" /></a> @@ -58,11 +61,6 @@ <table cellspacing="0" wicket:id="SearchPage.ResultatTestBios.Results"/> <a wicket:id="SearchPage.ResultatTestBios.ExportCSV"><wicket:message key="SearchPage.ExportCSV" /></a> -<h3><wicket:message key="SearchPage.Stations" /></h3> -<table cellspacing="0" wicket:id="SearchPage.Stations.Results"/> -<a wicket:id="SearchPage.Stations.ExportCSV"><wicket:message key="SearchPage.ExportCSV" /></a> -<!-- </div> --> - </wicket:extend> </body> </html> \ No newline at end of file
participants (1)
-
echatellier@users.forge.codelutin.com