package com.noveogroup.clap.converter;

import com.google.common.collect.Lists;
import com.noveogroup.clap.config.ConfigBean;
import com.noveogroup.clap.entity.message.BaseMessageEntity;
import com.noveogroup.clap.entity.revision.RevisionEntity;
import com.noveogroup.clap.entity.user.UserEntity;
import com.noveogroup.clap.model.message.BaseMessage;
import com.noveogroup.clap.model.revision.Revision;
import com.noveogroup.clap.model.user.ClapPermission;
import com.noveogroup.clap.model.user.User;
import com.noveogroup.clap.model.user.UserCreationModel;
import com.noveogroup.clap.model.user.UserWithPersistedAuth;

import java.util.List;

/**
 * @author Andrey Sokolov
 */
public class UserConverter {

    private RevisionConverter revisionConverter = new RevisionConverter();

    private MessagesConverter messagesConverter = new MessagesConverter();

    public User map(final UserEntity userEntity,final ConfigBean configBean) {
        final User ret = new User();
        map(ret, userEntity,configBean);
        return ret;
    }

    public UserEntity map(final UserCreationModel user) {
        final UserEntity ret = new UserEntity();
        ret.setLogin(user.getLogin());
        ret.setFullName(user.getFullName());
        ret.setHashedPassword(user.getPassword());
        ret.setClapPermissions(user.getClapPermissions());
        ret.setRole(user.getRole());
        return ret;
    }

    public void setRevisionConverter(final RevisionConverter revisionConverter) {
        this.revisionConverter = revisionConverter;
    }

    public void setMessagesConverter(final MessagesConverter messagesConverter) {
        this.messagesConverter = messagesConverter;
    }

    public UserWithPersistedAuth mapWithPersistedAuth(final UserEntity userEntity,final ConfigBean configBean) {
        final UserWithPersistedAuth ret = new UserWithPersistedAuth();
        map(ret, userEntity,configBean);
        ret.setToken(userEntity.getToken());
        return ret;
    }

    private void map(final User toMap, final UserEntity mapWith,final ConfigBean configBean) {
        toMap.setLogin(mapWith.getLogin());
        toMap.setFullName(mapWith.getFullName());
        toMap.setRole(mapWith.getRole());
        List<ClapPermission> permissions = Lists.newArrayList();
        permissions.addAll(mapWith.getClapPermissions());
        toMap.setClapPermissions(permissions);
        List<BaseMessage> messages = Lists.newArrayList();
        toMap.setUploadedMessages(messages);
        for (BaseMessageEntity messageEntity : mapWith.getUploadedMessages()) {
            messages.add(messagesConverter.map(messageEntity,configBean));
        }
        List<Revision> mainRevisions = Lists.newArrayList();
        toMap.setUploadedMainRevisions(mainRevisions);
        for (RevisionEntity revisionEntity : mapWith.getUploadedMainRevisions()) {
            mainRevisions.add(revisionConverter.map(revisionEntity,false,configBean));
        }
        List<Revision> specialRevisions = Lists.newArrayList();
        toMap.setUploadedSpecialRevisions(specialRevisions);
        for (RevisionEntity revisionEntity : mapWith.getUploadedSpecialRevisions()) {
            specialRevisions.add(revisionConverter.map(revisionEntity,false,configBean));
        }
    }
}

@@ -893,32 +893,33 @@
 tity userEntity,
+ 
 final ConfigBean
@@ -990,32 +990,33 @@
 ret, userEntity,
+ 
 configBean);%0A   
@@ -1768,16 +1768,17 @@
 rEntity,
+ 
 final Co
@@ -1895,24 +1895,25 @@
  userEntity,
+ 
 configBean);
@@ -2049,16 +2049,17 @@
 mapWith,
+ 
 final Co
@@ -2274,32 +2274,88 @@
 newArrayList();%0A
+        if (mapWith.getClapPermissions() != null) %7B%0A    
         permissi
@@ -2388,32 +2388,42 @@
 Permissions());%0A
+        %7D%0A
         toMap.se
@@ -2553,24 +2553,81 @@
 (messages);%0A
+        if (mapWith.getUploadedMessages() != null) %7B%0A    
         for 
@@ -2710,21 +2710,40 @@
     
-messages.add(
+    final BaseMessage message = 
 mess
@@ -2774,16 +2774,17 @@
 eEntity,
+ 
 configBe
@@ -2778,34 +2778,147 @@
 ity, configBean)
-)
 ;
+%0A                if (message != null) %7B%0A                    messages.add(message);%0A                %7D%0A            %7D
 %0A        %7D%0A     
@@ -3020,32 +3020,94 @@
 mainRevisions);%0A
+        if (mapWith.getUploadedMainRevisions() != null) %7B%0A    
         for (Rev
@@ -3189,26 +3189,38 @@
     
-mainRevisions.add(
+    final Revision revision = 
 revi
@@ -3244,38 +3244,40 @@
 (revisionEntity,
+ 
 false,
+ 
 configBean));%0A  
@@ -3263,34 +3263,154 @@
 lse, configBean)
-)
 ;
+%0A                if (revision != null) %7B%0A                    mainRevisions.add(revision);%0A                %7D%0A            %7D
 %0A        %7D%0A     
@@ -3521,32 +3521,97 @@
 cialRevisions);%0A
+        if (mapWith.getUploadedSpecialRevisions() != null) %7B%0A    
         for (Rev
@@ -3692,31 +3692,34 @@
         
-speci
+    fin
 al
+ 
 Revision
 s.add(re
@@ -3702,38 +3702,44 @@
   final Revision
-s.add(
+ revision = 
 revisionConverte
@@ -3759,22 +3759,24 @@
 nEntity,
+ 
 false,
+ 
 configBe
@@ -3778,18 +3778,141 @@
 figBean)
-)
 ;
+%0A                if (revision != null) %7B%0A                    specialRevisions.add(revision);%0A                %7D%0A            %7D
 %0A       

fix npe