Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / input / entitycreator / UserCreator.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.bulkeditor.input.entitycreator;
10
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 import javax.validation.ConstraintViolationException;
16
17 import org.springframework.security.access.AccessDeniedException;
18
19 import eu.etaxonomy.cdm.api.service.IUserService;
20 import eu.etaxonomy.cdm.model.permission.User;
21 import eu.etaxonomy.cdm.persistence.query.MatchMode;
22 import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
23 import eu.etaxonomy.taxeditor.l10n.Messages;
24 import eu.etaxonomy.taxeditor.model.MessagingUtils;
25 import eu.etaxonomy.taxeditor.store.CdmStore;
26
27 /**
28 * @author n.hoffmann
29 * @created Mar 9, 2011
30 */
31 public class UserCreator implements IEntityCreator<User>{
32
33 public static final String USER = "User"; //$NON-NLS-1$
34
35 @Override
36 public User createEntity(String text) {
37 return createEntity(null, text);
38 }
39
40 @Override
41 public User createEntity(Object key, String text) {
42
43 try{
44 User user = User.NewInstance(text, text);
45 List<User> userList =CdmStore.getService(IUserService.class).listByUsername(user.getUsername(), MatchMode.EXACT, null, 100, 0, null, null);
46 if (userList.isEmpty()){
47 CdmStore.getService(IUserService.class).createUser(user);
48 user = CdmStore.getService(IUserService.class).loadWithUpdate(user.getUuid());
49 return user;
50 } else{
51 MessagingUtils.messageDialog(Messages.USER_CREATOR_user_exists_title, user, Messages.USER_CREATOR_user_exists);
52 return null;
53 }
54 } catch (AccessDeniedException e){
55 MessagingUtils.messageDialog(Messages.USER_CREATOR_Acces_denied, getClass(), e.getMessage(), e);
56 return null;
57 } catch (ConstraintViolationException cve){
58 MessagingUtils.messageDialog(Messages.USER_CREATOR_Name_not_accepted, getClass(), Messages.USER_CREATOR_Name_not_accepted_message);
59 return null;
60 }
61
62 }
63
64 @Override
65 public Map<Object, String> getKeyLabelPairs() {
66 Map<Object, String> result = new HashMap<Object, String>();
67 result.put(User.class, USER);
68 return result;
69 }
70
71 @Override
72 public boolean savesEntity() {
73 // TODO Auto-generated method stub
74 return false;
75 }
76 }