首先,在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. java 连接oracle数据库

    package shujuku; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepared ...

  2. git-SSH连接配置

    1.1 在电脑端生成sshkey文件 打开git bash 输入: ssh-keygen -t rsa -C "xxxxxxx邮箱地址" 期间要输入两次密码[github远程添加s ...

  3. GIT学习地址

    https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 git下载:https://gi ...

  4. [转]java 中的序列化是什么意思?有什么好处?

    1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态,并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存Object States,但是Java给你提供一种应该比 ...

  5. 记录启用HTTPS的全过程

    因为 https 采用 ssl 加密,所以部署 https 时需要申请证书,证书的作用就是对浏览器和Web服务器双方的身份验证. 步骤1:申请证书 我们采用Let's Encrypt签发的免费证书,虽 ...

  6. 蓝桥杯 算法训练 ALGO-140 P1101

    算法训练 P1101 时间限制:1.0s 内存限制:256.0MB    有一份提货单,其数据项目有:商品名(MC).单价(DJ).数量(SL).定义一个结构体prut,其成员是上面的三项数据.在主函 ...

  7. bash的使用

    转自:http://blog.csdn.net/y2888886/article/details/50535033 在上篇博文的基础上做如下修改 注意一些常见命令中间就要加 “ ” ,否则很多命令无法 ...

  8. Day3-Python基础3--函数参数及调用

    一.return返回值 return的两个作用: 1)需要用一个变量来接受程序结束后返回的结果 2)它是作为一个结束符,终止程序运行 def test(): print("我是return前 ...

  9. mjpg-streamer在Ubuntu下编译,运行

    1.将USB摄像头插上,查看是否找到设备,输入: wp@ubuntu:/home/$    ls   /dev/video* /dev/video0 2.安装必要的软件集: sudo apt-get ...

  10. Rails:rails链接多个数据库【转】

    之前查到Rails应用中连接多个数据库的方式如下: class Cookie < ActiveRecord::Base establish_connection :typo ... end 这样 ...