01/01: improve import favorite list
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository pollen. See http://git.chorem.org/pollen.git commit ee941b8527d6ab15bd27fb4a8663bd3a307b8cd0 Author: Tony CHEMIT <chemit@codelutin.com> Date: Thu Jun 5 18:01:54 2014 +0200 improve import favorite list --- .../services/service/FavoriteListImport.java | 25 ++- .../service/FavoriteListImportFromFile.java | 174 ++++++++++++++++++- .../service/FavoriteListImportFromLdap.java | 144 ++++++++++++++- .../services/service/FavoriteListService.java | 193 +-------------------- .../services/service/PollenServiceSupport.java | 6 + 5 files changed, 344 insertions(+), 198 deletions(-) diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImport.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImport.java index 83a8a63..6a59535 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImport.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImport.java @@ -1,10 +1,33 @@ package org.chorem.pollen.services.service; +/* + * #%L + * Pollen :: Service + * %% + * Copyright (C) 2009 - 2014 CodeLutin + * %% + * 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% + */ + /** * Created on 6/5/14. * * @author Tony Chemit <chemit@codelutin.com> - * @since XXX + * @since 2.0 */ public interface FavoriteListImport { + + void doImport(String favoriteListId) throws FavoriteListImportException; } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImportFromFile.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImportFromFile.java index ae5010e..c997630 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImportFromFile.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImportFromFile.java @@ -1,10 +1,180 @@ package org.chorem.pollen.services.service; +/* + * #%L + * Pollen :: Service + * %% + * Copyright (C) 2009 - 2014 CodeLutin + * %% + * 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% + */ + +import com.google.common.base.Charsets; +import com.google.common.collect.Sets; +import com.google.common.io.Files; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.persistence.entity.FavoriteList; +import org.chorem.pollen.persistence.entity.FavoriteListMember; +import org.chorem.pollen.persistence.entity.PollenUser; +import org.chorem.pollen.services.PollenTechnicalException; +import org.nuiton.util.StringUtil; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Locale; +import java.util.Set; + +import static org.nuiton.i18n.I18n.l; + /** * Created on 6/5/14. * * @author Tony Chemit <chemit@codelutin.com> - * @since XXX + * @since 2.0 */ -public class FavoriteListImportFromFile implements FavoriteListImport { +public class FavoriteListImportFromFile extends PollenServiceSupport implements FavoriteListImport { + + /** Logger. */ + private static final Log log = LogFactory.getLog(FavoriteListImportFromFile.class); + + protected File file; + + public void setFile(File file) { + this.file = file; + } + + @Override + public void doImport(String favoriteListId) throws FavoriteListImportException { + + checkIsConnected(); + checkNotNull(favoriteListId); + checkNotNull(file); + checkState(file.exists()); + + PollenUser connectedUser = getConnectedUser(); + + FavoriteList favoriteList = getFavoriteListService().getFavoriteList0(connectedUser, favoriteListId); + + Locale locale = serviceContext.getLocale(); + + Set<String> usedName = Sets.newHashSet(); + Set<String> usedEmail = Sets.newHashSet(); + + List<FavoriteListMember> favoriteListMembers = getFavoriteListService().getFavoriteListMembers0(favoriteList); + + if (CollectionUtils.isNotEmpty(favoriteListMembers)) { + for (FavoriteListMember member : favoriteListMembers) { + usedName.add(member.getName()); + usedEmail.add(member.getEmail()); + } + } + + int result = 0; + BufferedReader reader = null; + try { + + reader = Files.newReader(file, Charsets.UTF_8); + String line; + int lineNumber = 0; + + while ((line = reader.readLine()) != null) { + + line = line.trim(); + + if (StringUtils.isBlank(line) || line.startsWith("#")) { + + // comment line + continue; + + } + lineNumber++; + result++; + + int spaceIndex = line.indexOf(' '); + String email; + String memberName; + + if (spaceIndex == -1) { + + // only email + email = line; + memberName = line; + + } else { + + // email + name + email = line.substring(0, spaceIndex); + memberName = line.substring(spaceIndex); + + } + memberName = memberName.trim(); + + if (!usedName.add(memberName)) { + // name already exists + String error = l(locale, "pollen.error.favoriteList.import.csv.already.used.name", lineNumber, memberName); + throw new FavoriteListImportException(error, null); + + } + + email = email.toLowerCase().trim(); + if (!usedEmail.add(email)) { + // email already exists + String error = l(locale, "pollen.error.favoriteList.import.csv.already.used.email", lineNumber, email); + throw new FavoriteListImportException(error, null); + + } + + if (!StringUtil.isEmail(email)) { + + // email is not valid + String error = l(locale, "pollen.error.favoriteList.import.csv.invalid.email", lineNumber, email); + throw new FavoriteListImportException(error, null); + + } + + FavoriteListMember member = getFavoriteListMemberDao().newInstance(); + member.setName(memberName); + member.setEmail(email); + member.setFavoriteList(favoriteList); + + } + + reader.close(); + + } catch (IOException e) { + + // should never happens ? + throw new PollenTechnicalException(e); + + } finally { + + IOUtils.closeQuietly(reader); + + } + + if (log.isInfoEnabled()) { + log.info("Imported members: " + result); + } + + commit(); + + } } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImportFromLdap.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImportFromLdap.java index f601f03..2e3c964 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImportFromLdap.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListImportFromLdap.java @@ -1,37 +1,165 @@ package org.chorem.pollen.services.service; +/* + * #%L + * Pollen :: Service + * %% + * Copyright (C) 2009 - 2014 CodeLutin + * %% + * 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% + */ + +import com.google.common.collect.Sets; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.chorem.pollen.persistence.entity.FavoriteList; +import org.chorem.pollen.persistence.entity.FavoriteListMember; import org.chorem.pollen.persistence.entity.PollenUser; +import org.nuiton.util.StringUtil; + +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.DirContext; +import javax.naming.directory.InitialDirContext; +import javax.naming.directory.SearchControls; +import javax.naming.directory.SearchResult; +import java.util.List; +import java.util.Locale; +import java.util.Properties; +import java.util.Set; -import java.io.File; +import static org.nuiton.i18n.I18n.l; /** * Created on 6/5/14. * * @author Tony Chemit <chemit@codelutin.com> - * @since XXX + * @since 2.0 */ public class FavoriteListImportFromLdap extends PollenServiceSupport implements FavoriteListImport { - protected File importfile; + /** Logger. */ + private static final Log log = LogFactory.getLog(FavoriteListImportFromLdap.class); - public void setImportfile(File importfile) { - this.importfile = importfile; + protected String ldap; + + public void setLdap(String ldap) { + this.ldap = ldap; } @Override - public void doImport(String favoriteListId) { + public void doImport(String favoriteListId) throws FavoriteListImportException { checkIsConnected(); checkNotNull(favoriteListId); - checkNotNull(importfile); - checkState(importfile.exists()); + checkNotNull(ldap); PollenUser connectedUser = getConnectedUser(); FavoriteList favoriteList = getFavoriteListService().getFavoriteList0(connectedUser, favoriteListId); + Locale locale = serviceContext.getLocale(); + + Set<String> usedName = Sets.newHashSet(); + Set<String> usedEmail = Sets.newHashSet(); + + List<FavoriteListMember> favoriteListMembers = getFavoriteListService().getFavoriteListMembers0(favoriteList); + + if (CollectionUtils.isNotEmpty(favoriteListMembers)) { + for (FavoriteListMember member : favoriteListMembers) { + usedName.add(member.getName()); + usedEmail.add(member.getEmail()); + } + } + + int result = 0; + + try { + + // Initialisation du contexte + Properties env = new Properties(); + + DirContext ictx = new InitialDirContext(env); + + // Recherche en profondeur + SearchControls control = new SearchControls(); + control.setSearchScope(SearchControls.SUBTREE_SCOPE); + + // Création des comptes avec les résultats de la recherche + NamingEnumeration<SearchResult> e = ictx.search(ldap, null, control); + + while (e.hasMore()) { + + SearchResult r = e.next(); + + Attribute attrName = r.getAttributes().get("cn"); + Attribute attrEmail = r.getAttributes().get("mail"); + + if (attrName != null) { + + result++; + + String memberName = attrName.get().toString().trim(); + + if (!usedName.add(memberName)) { + + // name already exists + String error = l(locale, "pollen.error.favoriteList.import.ldap.already.used.name", memberName); + throw new FavoriteListImportException(error, null); + + } + + String email = attrEmail.get().toString().toLowerCase().trim(); + + if (!usedEmail.add(email)) { + + // email already exists + String error = l(locale, "pollen.error.favoriteList.import.ldap.already.used.email", email); + throw new FavoriteListImportException(error, null); + + } + + if (!StringUtil.isEmail(email)) { + + // email is not valid + String error = l(locale, "pollen.error.favoriteList.import.ldap.invalid.email", email); + throw new FavoriteListImportException(error, null); + + } + + FavoriteListMember member = getFavoriteListMemberDao().newInstance(); + member.setName(memberName); + member.setEmail(email); + member.setFavoriteList(favoriteList); + + } + } + + } catch (NamingException ex) { + + throw new FavoriteListImportException("LDAP", ex); + + } + + if (log.isInfoEnabled()) { + log.info("Imported members: " + result); + } commit(); + } } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java index aabf7af..0f5b53e 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java @@ -25,8 +25,6 @@ package org.chorem.pollen.services.service; import com.google.common.collect.Sets; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.persistence.entity.FavoriteList; @@ -34,34 +32,18 @@ import org.chorem.pollen.persistence.entity.FavoriteListMember; import org.chorem.pollen.persistence.entity.FavoriteListMemberTopiaDao; import org.chorem.pollen.persistence.entity.FavoriteListTopiaDao; import org.chorem.pollen.persistence.entity.PollenUser; -import org.chorem.pollen.services.PollenTechnicalException; import org.chorem.pollen.services.bean.FavoriteListBean; import org.chorem.pollen.services.bean.FavoriteListMemberBean; import org.chorem.pollen.services.bean.PaginationParameterBean; import org.chorem.pollen.services.bean.PaginationResultBean; import org.chorem.pollen.services.bean.PollenEntityRef; -import org.nuiton.util.StringUtil; import org.nuiton.util.pagination.PaginationParameter; import org.nuiton.util.pagination.PaginationResult; -import javax.naming.NamingEnumeration; -import javax.naming.NamingException; -import javax.naming.directory.Attribute; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; -import javax.naming.directory.SearchControls; -import javax.naming.directory.SearchResult; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.IOException; import java.util.List; -import java.util.Locale; -import java.util.Properties; import java.util.Set; -import static org.nuiton.i18n.I18n.l; - /** * TODO * @@ -272,94 +254,9 @@ public class FavoriteListService extends PollenServiceSupport { checkNotNull(favoriteListId); checkNotNull(file); - PollenUser user = getConnectedUser(); - - FavoriteList favoriteList = getFavoriteList0(user, favoriteListId); - - Locale locale = serviceContext.getLocale(); - - Set<String> usedName = Sets.newHashSet(); - Set<String> usedEmail = Sets.newHashSet(); - - List<FavoriteListMember> favoriteListMembers = getFavoriteListMembers0(favoriteList); - - if (CollectionUtils.isNotEmpty(favoriteListMembers)) { - for (FavoriteListMember member : favoriteListMembers) { - usedName.add(member.getName()); - usedEmail.add(member.getEmail()); - } - } - - int result = 0; - BufferedReader reader = null; - try { - reader = new BufferedReader(new FileReader(file)); - String line; - int lineNumber = 0; - while ((line = reader.readLine()) != null) { - line = line.trim(); - if (StringUtils.isBlank(line) || line.startsWith("#")) { - - // comment line - continue; - } - lineNumber++; - result++; - - int spaceIndex = line.indexOf(' '); - String email; - String memberName; - if (spaceIndex == -1) { - // only email - email = line; - memberName = line; - } else { - // email + name - email = line.substring(0, spaceIndex); - memberName = line.substring(spaceIndex); - } - memberName = memberName.trim(); - - if (!usedName.add(memberName)) { - // name already exists - String error = l(locale, "pollen.error.favoriteList.import.csv.already.used.name", lineNumber, memberName); - throw new FavoriteListImportException(error, null); - } - - email = email.toLowerCase().trim(); - if (!usedEmail.add(email)) { - // email already exists - String error = l(locale, "pollen.error.favoriteList.import.csv.already.used.email", lineNumber, email); - throw new FavoriteListImportException(error, null); - } - - if (!StringUtil.isEmail(email)) { - - // email is not valid - String error = l(locale, "pollen.error.favoriteList.import.csv.invalid.email", lineNumber, email); - throw new FavoriteListImportException(error, null); - } - - FavoriteListMember member = getFavoriteListMemberDao().newInstance(); - member.setName(memberName); - member.setEmail(email); - member.setFavoriteList(favoriteList); - } - - reader.close(); - - } catch (IOException e) { - // should never happens ? - throw new PollenTechnicalException(e); - } finally { - IOUtils.closeQuietly(reader); - } - - if (log.isInfoEnabled()) { - log.info("Imported members: " + result); - } - - commit(); + FavoriteListImportFromFile importer = new FavoriteListImportFromFile(); + importer.setFile(file); + importer.doImport(favoriteListId); } @@ -370,87 +267,9 @@ public class FavoriteListService extends PollenServiceSupport { checkNotNull(favoriteListId); checkNotNull(ldap); - PollenUser user = getConnectedUser(); - - FavoriteList favoriteList = getFavoriteList0(user, favoriteListId); - - Locale locale = serviceContext.getLocale(); - - Set<String> usedName = Sets.newHashSet(); - Set<String> usedEmail = Sets.newHashSet(); - - List<FavoriteListMember> favoriteListMembers = getFavoriteListMembers0(favoriteList); - - if (CollectionUtils.isNotEmpty(favoriteListMembers)) { - for (FavoriteListMember member : favoriteListMembers) { - usedName.add(member.getName()); - usedEmail.add(member.getEmail()); - } - } - - int result = 0; - - try { - - // Initialisation du contexte - Properties env = new Properties(); - - DirContext ictx = new InitialDirContext(env); - - // Recherche en profondeur - SearchControls control = new SearchControls(); - control.setSearchScope(SearchControls.SUBTREE_SCOPE); - - // Création des comptes avec les résultats de la recherche - NamingEnumeration<SearchResult> e = ictx.search(ldap, null, control); - while (e.hasMore()) { - SearchResult r = e.next(); - - Attribute attrName = r.getAttributes().get("cn"); - Attribute attrEmail = r.getAttributes().get("mail"); - - if (attrName != null) { - - result++; - - String memberName = attrName.get().toString().trim(); - - if (!usedName.add(memberName)) { - // name already exists - String error = l(locale, "pollen.error.favoriteList.import.ldap.already.used.name", memberName); - throw new FavoriteListImportException(error, null); - } - - String email = attrEmail.get().toString().toLowerCase().trim(); - - if (!usedEmail.add(email)) { - // email already exists - String error = l(locale, "pollen.error.favoriteList.import.ldap.already.used.email", email); - throw new FavoriteListImportException(error, null); - } - - if (!StringUtil.isEmail(email)) { - - // email is not valid - String error = l(locale, "pollen.error.favoriteList.import.ldap.invalid.email", email); - throw new FavoriteListImportException(error, null); - } - - FavoriteListMember member = getFavoriteListMemberDao().newInstance(); - member.setName(memberName); - member.setEmail(email); - member.setFavoriteList(favoriteList); - } - } - } catch (NamingException ex) { - throw new FavoriteListImportException("LDAP", ex); - } - - if (log.isInfoEnabled()) { - log.info("Imported members: " + result); - } - - commit(); + FavoriteListImportFromLdap importer = new FavoriteListImportFromLdap(); + importer.setLdap(ldap); + importer.doImport(favoriteListId); } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenServiceSupport.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenServiceSupport.java index 77e8a12..da78c96 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenServiceSupport.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/PollenServiceSupport.java @@ -275,6 +275,12 @@ public abstract class PollenServiceSupport implements PollenService { } + protected void checkState(boolean test) { + + Preconditions.checkState(test); + + } + protected boolean check(Multimap<String, String> errors, String field, boolean condition, String error) { boolean valid = condition; -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
participants (1)
-
chorem.org scm