首先,在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. php 利用header 函数 下载各种文件

    http://www.php.net/manual/en/function.readfile.php <?php /** * 下载文件 * header函数 * */ dl_file($_GET ...

  2. bzoj 1607 [Usaco2008 Dec]Patting Heads 轻拍牛头——枚举倍数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1607 #include<iostream> #include<cstdio ...

  3. MSSQL Join的使用

    假设我们有下面两张表.表A在左边,表B在右边.我们给它们各四条记录. id name id name -- ---- -- ---- 1 Pirate 1 Rutabaga 2 Monkey 2 Pi ...

  4. 解决windows下tomcat端口被占用[Address already in use: JVM_Bind]

    有时候电脑比较卡,项目比较大的情况下,eclipse没有完全停止tomcat的情况下,下次启动会出现tomcat的端口被占用无法启动的情况,主要报如下错误 解决方案 window下打开命令窗口(快捷键 ...

  5. 在Mac系统下使用自己安装的PHP

    今天在安装单元测试框架PHPUnit,需要PHP的最低版本是5.6,由于我的MacBook自带的PHP版本是5.5.36,不能满足安装条件. 看了一下这个网址:https://php-osx.liip ...

  6. Codeforce 101B. Buses(线段树or树状数组+离散化)

     Buses                                                                                               ...

  7. 学习vue

    一,声明模板的时候需要新建示例 如下代码 <div id="app"> <my></my> </div> Vue.component ...

  8. TCG卡牌游戏研究:《炉石战记:魔兽英雄传》所做的改变

    转自:http://www.gameres.com/665306.html TCG演进史 说到卡牌游戏,大家会联想到什么呢? 是历史悠久的扑克牌.风靡全球的<MTG 魔法风云会>与< ...

  9. mybatis 学习三 关键文件解析

    1:  mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP ...

  10. 2016.8.17服务器端数据库用户导入导出方法 expdp和impdp

    EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用. IMP只适用于EXP导出的 ...