首先,在PersonRepository的父类中查找save方法,如下:

@Override
    @TransactionalMethod
    public <S extends D> S save(S dto) {
        if (getApiContext().isPostingRelated()) {
            if (this.getResourceConfiguration().isSecondLevelPostDisabled()) {
                throw new UnsupportedRequestException("Current resource " + this.getResourceConfiguration().getResourceType() + " not supported second level post");
            }
            getApiContext().setDtoRelations(dto);
        }
        this.validate(dto);
        final String id = dto.getId();
        if (id == null) {
            Object entity = this.createEntity(dto);
            this.updateEntity(dto, entity);
            return (S) this.createDto(entity, dto);
        } else {
            return (S) this.updateEntity(dto, findEntity(id));
        }
    }

分析以上代码,理解:

如果id未null,则首先,新建一个entity实体,然后再更新entity字段值。

如果该id存在,则直接更新数据库中的entity的值。

故,为了实现该功能,需要在PersonRepository类重写createEntity方法。

    protected Person createEntity(PersonDto dto) {
        //暂时让contactSource定义为null,创建person后再塞值
       Person person = crmManager.createPerson(dto.getFirstName(),dto.getLastName(),null);
       return person;
    }

这里,第三个contactSource参数的传递,sunny思考了很久,因为,contactSource是个实体,它的构造器有点麻烦。不知道如何下手。

后,转化思路,首先可以传个null值,创建出来一个实体entity,把其他字段比如基本属性将post请求先做完。

于是,再查看父类方法:

protected D updateEntity(D dto, Object entity) {
        this.getConverter().updateEntity(dto, entity);

        this.repositoryEventListeners.forEach(listener -> listener.postUpdateEntity(dto, entity));

        return dto;
    }

点击,进入updateEntity方法,发现它是个接口,找到自己的实现类PersonConverter,重写其方法:

@Override
    public void updateEntity(PersonDto dto, Person entity) {

        String cellularNumber = dto.getMobilePhone();
        entity.setCellularNumber(PhoneNumberUtilWrapperImpl.getInstance().parse(cellularNumber));
        entity.setDepartment(dto.getDepartment());
        entity.setTitle(dto.getTitle());
        entity.setOtherId(dto.getOtherId());
        entity.setBirthdate(dto.getBirthdate());
        entity.setFirstName(dto.getFirstName());
        entity.setLastName(dto.getLastName());
        entity.setGender(dto.getGender());
        entity.setFundEmployee(dto.isFundEmployee());
        entity.setEmailHidden(dto.isEmailHidden());
        entity.setSyncDisabled(dto.isSyncDisabled());
        entity.setSuffix(dto.getSuffix());
        entity.setSalutation(dto.getSalutation());
        entity.setPronunciation(dto.getPronunciation());
        String website = dto.getWebsite();
        if (!ValidationUtils.isWebsiteValid(website)) {
            throw new IllegalArgumentException("Invalid website " + website);
        }
        entity.setWebsite(website);
        entity.setPartyDescription(dto.getDescription());

    }

就这样,sunny将personDto的基本属性都已经完成了post和patch请求。接下来,要完成,比较复杂的字段对应的post请求。

之所以复杂,是因为它的类型是个实体,而该实体的构造器需要的参数,不好获取。也许获取不到。总之,在sunny刚开始动手写代码的时候,很头疼。

经过不断的思考以及查看代码和debug分析,最终,从action开始入手。

找到前端界面对应的ManageContactAction,搜createPerson,找到关键代码:

if (CREATE_PERSON_ACTION.equals(action)) {
                if(!getPermissionManager().hasPermission(getUserManager().getCurrentUser(), PermissionLevel.MODIFY, Party.class)){
                    return new ActionRedirect("/backstop/index.jsp");
                }

                if (!form.getBoolean(IGNORE_WARNING)) {
                    String firstName = StringUtils.defaultString(form.getString("firstName")).trim();
                    String lastName = form.getString("lastName").trim();
                    Collection allByName = getCrmManager().wormHoleFindPartiesByName(firstName + ' ' + lastName);
                    if (!allByName.isEmpty()) {
                        // Warn the user that they are creating (maybe) creating a duplicate person
                        form.set(IGNORE_WARNING, "true");
                        request.setAttribute(CATEGORIES, getCategories(request));
                        request.setAttribute(ALL_CATEGORIES, CategorizeContacts.getModifiableCategories());
                        request.setAttribute(IS_CREATE_ATTR, Boolean.TRUE);
                        request.setAttribute(SIMILAR_PARTIES, allByName);
                        loadDefaultInformation(request, form);
                        return actionMapping.findForward("edit_person");
                    }
                }
                Person person = createPerson(request, form);
                RecentlyViewed.addRecent(request, response, person);
                if (Boolean.valueOf(request.getParameter(ADD_EMPLOYEE))) {
                    addEmployeeToOrganization(person, request.getParameter(COMPANY_NAME));
                }
                form.reset(actionMapping);
                return handleDisplay(person, SUMMARY_TYPE, request, response, actionMapping);

举个例子,sunny想实现contactSource的post请求,则,直接在ManageContactAction中搜:contactSource,然后就能找到对应的关键代码:

@TransactionalMethod
    private Person createPerson(HttpServletRequest request, ManageContactsForm form) throws UserInputException {
        Code contactSource = CodeLookup.getInstance().getContactSourceCode(null);
        if ( ! StringUtils.isEmpty(form.getString(CONTACT_SOURCE_ATTR)) ) {
            contactSource = CodeLookup.getInstance().getContactSourceCode((form.getInteger(CONTACT_SOURCE_ATTR)));
        }
        Person person = getCrmManager().createPerson(form.getString("lastName"), form.getString("firstName"),
                contactSource);
        Integer employerPartyId = form.getInteger(EMPLOYER_PARTY_ID);
        if (!employerPartyId.equals(NumberUtil.INTEGER_ZERO)) {
            Organization organization;
            try {
                organization = getCrmManager().findOrganizationById(employerPartyId);
            } catch (PartyNotFoundException e) {
                logger.error("Problem: " + e.getMessage(), e);
                throw new SystemException("Problem: " + e.getMessage(), e);
            }
            organization.addEmployee(person);
        }

        editContact(form, person, (form.get(LOCATIONS) != null), request);
        return person;
    }

需要注意的是,在post请求的时候,这里传入的contactSource是前端字符串对应的value值,也就是contactSource的id值。---这一点,感谢simon帮我一起分析。

结合以上代码,sunny的代码重构如下:

@Override
    public void updateEntity(PersonDto dto, Person entity) {

        //there ,ContactSourceName() is a responding contactSource Code.
        if(!Objects.isNull(dto.getContactSourceName())){
            int contactSourceCode = Integer.parseInt(dto.getContactSourceName());
            Code contactSource = CodeLookup.getInstance().getContactSourceCode(contactSourceCode);
            entity.setContactSource(contactSource);
        }

        String cellularNumber = dto.getMobilePhone();
        entity.setCellularNumber(PhoneNumberUtilWrapperImpl.getInstance().parse(cellularNumber));
        entity.setDepartment(dto.getDepartment());
        entity.setTitle(dto.getTitle());
        entity.setOtherId(dto.getOtherId());
        entity.setBirthdate(dto.getBirthdate());
        entity.setFirstName(dto.getFirstName());
        entity.setLastName(dto.getLastName());
        entity.setGender(dto.getGender());
        entity.setFundEmployee(dto.isFundEmployee());
        entity.setEmailHidden(dto.isEmailHidden());
        entity.setSyncDisabled(dto.isSyncDisabled());
        entity.setSuffix(dto.getSuffix());
        entity.setSalutation(dto.getSalutation());
        entity.setPronunciation(dto.getPronunciation());
        String website = dto.getWebsite();
        if (!ValidationUtils.isWebsiteValid(website)) {
            throw new IllegalArgumentException("Invalid website " + website);
        }
        entity.setWebsite(website);
        entity.setPartyDescription(dto.getDescription());

    }

其他复杂的字段,类似操作如上。sunny就不再一一例举了。

Person.post请求------详细过程的更多相关文章

  1. Asp.net页面生命周期详解任我行(3)-服务器处理请求详细过程

    前言 百度了一下才知道,传智的邹老师桃李满天下呀,我也是邹老师的粉丝,最开始学习页面生命周期的时候也是看了邹老师的视频. 本人是参考了以下前辈的作品,本文中也参合了本人心得,绝非有意盗版,旨在传播,最 ...

  2. Person的delete请求--------详细过程

    首先,数据库的增删改查都是在PersonRepository中实现,因此,直接进入PersonRepository,找到其父类,搜索delete. @Override @TransactionalMe ...

  3. 在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程)

    在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程) 原文链接:http://www.360doc.com/content/14/1117/10/16948208_42571794 ...

  4. 一个http请求的详细过程

    一个http请求的详细过程 我们来看当我们在浏览器输入http://www.mycompany.com:8080/mydir/index.html,幕后所发生的一切. 首先http是一个应用层的协议, ...

  5. USB枚举详细过程剖析(转)

    USB枚举详细过程剖析(转) 原文地址:http://blog.163.com/luge_arm/blog/static/6774972620071018117290/ 从驱动开发网看到一篇<U ...

  6. HTTP请求响应过程 与HTTPS区别

    原文:HTTP请求响应过程 与HTTPS区别 HTTP协议学习笔记,基础,干货 HTTP协议 HTTP协议主要应用是在服务器和客户端之间,客户端接受超文本. 服务器按照一定规则,发送到客户端(一般是浏 ...

  7. mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)

    文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...

  8. IDEA搭建SSMM框架(详细过程)

    IDEA搭建SSMM框架(详细过程) 相关环境 Intellij IDEA Ultimate Tomcat JDK MySql 5.6(win32/win64) Maven (可使用Intellij ...

  9. Tomcat配置(三):tomcat处理连接的详细过程

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

随机推荐

  1. 不带缓存IO和标准(带缓存)IO

    linux对IO文件的操作分为: 不带缓存:open read.posix标准,在用户空间没有缓冲,在内核空间还是进行了缓存的.数据-----内核缓存区----磁盘 假设内核缓存区长度为100字节,你 ...

  2. redhat5.8 alt+ctrl+f1 黑屏

    /********************************************************************** * redhat5.8 alt+ctrl+f1 黑屏 * ...

  3. Redis底层探秘(五):Redis对象

    前面几篇文章,我们一起学习了redis用到的所有主要数据结构,比如简单动态字符串(sds).双端链表.字典.压缩列表.整数集合等等. redis并没有直接使用这些数据结构来实现键值对数据库,而是基于这 ...

  4. java 使用最新api操作mongodb

    // package com.auto.test.dbmodel; import java.util.ArrayList; import org.bson.Document;import org.bs ...

  5. hw_module_t 加载过程

    每一个HAL模块都有一个ID值,以这些ID值为参数来调用硬件抽象层提供的函数hw_get_module就可以将指定的模块加载到内存来,并且获得 一个hw_module_t接口来打开相应的设备. 函数h ...

  6. Operating System-进程间互斥的问题-生产者&&消费者引入

    之前介绍的几种解决进程间互斥的方案,不管是Peterson方案还是TSL指令的方式,都有一个特点:当一个进程被Block到临界区外面时,被Block的进程会一直处于忙等待的状态,这个不但浪费了CPU资 ...

  7. BZOJ4170:极光

    浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...

  8. 蓝桥杯 算法训练 ALGO-121 猴子分苹果

      算法训练 猴子分苹果   时间限制:1.0s   内存限制:256.0MB 问题描述 秋天到了,n只猴子采摘了一大堆苹果放到山洞里,约定第二天平分.这些猴子很崇拜猴王孙悟空,所以都想给他留一些苹果 ...

  9. (转)nodejs搭建本地http服务器

    本文转载自:http://www.cnblogs.com/shawn-xie/archive/2013/06/06/3121173.html 由于不做php相关的东西,懒得装apache,干脆利用no ...

  10. [转] Linux 查找文件内容

    Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件g ...