首先,在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. linux命令学习笔记(28):tar命令

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的 工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar命令可以为linu ...

  2. UVA - 10570 Meeting with Aliens (置换的循环节)

    给出一个长度不超过500的环状排列,每次操作可以交换任意两个数,求把这个排列变成有序的环状排列所需的最小操作次数. 首先把环状排列的起点固定使其成为链状排列a,枚举排好序时的状态b(一种有2n种可能) ...

  3. HihoCoder1105 题外话·堆(基础二叉搜索树)

    第1行为1个整数N,表示需要处理的事件数目. 接下来的M行,每行描述一个事件,且事件类型由该行的第一个字符表示,如果为'A',表示小Ho将一粒糖果放进了盒子,且接下来为一个整数W,表示这颗糖果的重量: ...

  4. SSL/TLS捕包分析

    一.基本概念 SSL:(Secure Socket Layer,安全套接字层),位于可靠的面向连接的网络层协议和应用层协议之间的一种协议层.SSL通过互相认证.使用数字签名确保完整性.使用加密确保私密 ...

  5. python函数之sorted与sort

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,revers ...

  6. Chroma Oracle 安装宝典,吐血整理

    尼玛,太坑爹!安装: 1.Chroma Application Service 2. PL SQL 安装Oracle 11g 的步骤和过程: 第一步:只能安装 Oracle 11g 64 bit 数据 ...

  7. Linux应用函数 -- 字符串

    1.strchr 原型 char *strchr(const char* _Str,char _Val) 头文件 string.h 功能 查找字符串_Str中首次出现字符_Val的位置 返回值  成功 ...

  8. ajax补充FormData

    一.回顾上节知识点 1.什么是json字符串? 轻量级的数据交换格式 2.定时器:关于setTimeout setTimeout(foo,3000)  # 3000表示3秒,foo表示一个函数,3秒后 ...

  9. MVC5网站部署到IIS7

    server 2008R2+IIS7.5下配置不会出现什么问题,这里记录下在server2008+IIS7下的配置 参考了一下:http://www.cnblogs.com/fcu3dx/p/3773 ...

  10. .net 4 安装未成功,无意中的解决办法!

    公司 电脑是chost的系统,由于使用时间过长,重装纯净版系统的话,代价太大,故网上寻求各种解决办法! 安装.net 4 总是失败,查看百度,各种: WIN7系统哈哈跟我的问题一样,我的刚才解决了:1 ...