比如:我们在做CRM系统的时候,CRM系统中的字段是根据用户自己的需求来自己设定的,我们首先提供一些,必须的几个常用的字段,特别的用户是根据自己公司的建制有自己需求的字段,那我们就要在设计数据库的时候提前预留出一些字段大概也就是20个左右的字段,让客户使用。那我们针对这个预留的字段怎么获取值啊?比如用户在界面上把预留字段strFiled1设置成“公司的文化”这个字段,那我们该如何处理界面上显示的这个字段所对应的值那?这个预留的字段的名(strField1)会不会被修改成(companyWenHua)这个名字啊?估计大家都会有这样的疑问,我刚开始也是怀揣这个疑问。下面直接上代码:
//得到Customer的值 public static Map<String, String> getCustomerValue(PortletRequest portletRequest, Long customerId) throws PortalException, SystemException { Map<String, String> dynamicValue = null; long companyId = PortalUtil.getCompanyId(portletRequest); if (customerId > 0) { Customer customer = null; try { customer = CustomerLocalServiceUtil.getCustomer(customerId); } catch (NoSuchCustomerException e) { _log.error("Exception : No Such Customer Exception " + customerId); customer = null; } if (customer != null) { List<CompanyColumnDefinition> companyColumns = CompanyColumnDefinitionLocalServiceUtil.searchByTableName(companyId, TableConst.CRM_Customer, true); dynamicValue = new HashMap<String, String>(); for(CompanyColumnDefinition companyColumn : companyColumns){ String columnName = companyColumn.getColumnName(); if (columnName.equals("code")) { dynamicValue.put(columnName, customer.getCode()); } else if (columnName.equals("customerName")) { dynamicValue.put(columnName, customer.getCustomerName() ); } else if (columnName.equals("customerType")) { String type = customer.getCustomerType(); dynamicValue.put(columnName, type == null?StringPool.BLANK:type); } else if (columnName.equals("source")){ String source = customer.getSource(); dynamicValue.put(columnName, source == null?StringPool.BLANK:source); } else if (columnName.equals("upCustomerId")) { String upName = StringPool.BLANK; long upId = customer.getUpCustomerId(); if (upId != 0L) { String prefix = "<a href='" + PortletURLUtil.getObjDetailURL( portletRequest, PortletIDs.CUSTOMER_DETAIL, upId) + "'>"; String suffix = "</a>"; upName = prefix + CustomerLocalServiceUtil .getCustomer(upId).getCustomerName() + suffix; } dynamicValue.put(columnName, upName ); } else if (columnName.equals("contactId")) { long contactId = customer.getContactId(); String contactName = StringPool.BLANK; if (contactId > 0) { String prefix = "<a href='" + PortletURLUtil.getObjDetailURL( portletRequest, PortletIDs.CONTACT_DETAIL, contactId) + "'>"; String suffix = "</a>"; contactName = prefix+ ContactLocalServiceUtil.getContact(contactId).getContactName() + suffix; } dynamicValue.put(columnName, contactName); } else if (columnName.equals("region")) { String region = customer.getRegion(); dynamicValue.put(columnName, region == null?StringPool.BLANK:region); } else if (columnName.equals("phone")) { dynamicValue.put(columnName, customer.getPhone()); } else if (columnName.equals("email")) { dynamicValue.put(columnName, customer.getEmail()); } else if (columnName.equals("mobile")) { dynamicValue.put(columnName, customer.getMobile()); } else if (columnName.equals("country")) { String country = customer.getCountry(); dynamicValue.put(columnName, country); } else if (columnName.equals("province")) { String province = customer.getProvince(); dynamicValue.put(columnName, province); } else if (columnName.equals("city")) { String city = customer.getCity(); dynamicValue.put(columnName, city); } else if (columnName.equals("area")) { String area = customer.getArea(); dynamicValue.put(columnName, area); } else if (columnName.equals("postCode")) { String postCode = customer.getPostCode(); dynamicValue.put(columnName, postCode == null ? StringPool.BLANK : postCode); } else if (columnName.equals("address")) { Locale locale = CompanyLocalServiceUtil.getCompany(companyId).getLocale(); String address = customer.getAddress(); if (!address.equals("")) { address += " <a class='baiduMap' style='color:#015BA7;' href='javascript:void(0);'>" + LanguageUtil.get( PortletIDs.INITIALIZATION, locale, "baidu.map") + "</a>"; } else { address += "<a class='baiduMap' style='color:#015BA7;' href='javascript:void(0);'>" + LanguageUtil.get( PortletIDs.INITIALIZATION, locale, "baidu.map.marker") + "</a>"; } dynamicValue.put(columnName, address); } else if (columnName.equals("ownerId")) { long ownerId = customer.getOwnerId(); String ownerName = StringPool.BLANK; if (ownerId != 0L) { ownerName = UserLocalServiceUtil.getUser(ownerId).getFirstName(); } dynamicValue.put(columnName, ownerName); } else if (columnName.equals("createUserId")) { long createUserId = customer.getCreateUserId(); StringBuffer createUser = new StringBuffer(StringPool.BLANK); if (createUserId != 0L) { createUser.append(UserLocalServiceUtil.getUser(createUserId).getFirstName()) .append(StringPool.COMMA) .append(DateUtil.formatDateTime(customer.getCreateDate())); } dynamicValue.put(columnName, createUser.toString()); } else if (columnName.equals("createDate")) { dynamicValue.put(columnName, DateUtil.formatDateTime(customer.getCreateDate())); } else if (columnName.equals("editUserId")) { long editUserId = customer.getEditUserId(); StringBuffer editUser = new StringBuffer(StringPool.BLANK); if (editUserId != 0L) { editUser.append(UserLocalServiceUtil.getUser(editUserId).getFirstName()) .append(StringPool.COMMA) .append(DateUtil.formatDateTime(customer.getEditDate())); } dynamicValue.put(columnName, editUser.toString()); } else if (columnName.equals("editDate")) { dynamicValue.put(columnName, DateUtil.formatDateTime(customer.getEditDate())); } else if (columnName.equals("ownerChangeDate")) { dynamicValue.put(columnName, DateUtil.formatDateTime(customer.getOwnerChangeDate())); } else if (columnName.equals("webSite")) { String webSite = customer.getWebSite() != null ? customer.getWebSite() : StringPool.BLANK; StringBuffer sb = new StringBuffer(StringPool.BLANK); sb.append("<a href=").append(StringPool.QUOTE).append("http://").append(webSite) .append(StringPool.QUOTE).append(StringPool.SPACE).append("target=") .append(StringPool.QUOTE).append("_blank").append(StringPool.QUOTE) .append(">").append(webSite).append("</a>"); dynamicValue.put(columnName, sb.toString()); } else if (columnName.equals("relationShip")) { String relationShip = customer.getRelationShip(); dynamicValue.put(columnName, relationShip == null ? StringPool.BLANK : relationShip); } else if (columnName.equals("industry")) { String industry = customer.getIndustry(); dynamicValue.put(columnName, industry == null ? StringPool.BLANK : industry); } else if (columnName.equals("staffNum")) { String staffNum = customer.getStaffNum(); dynamicValue.put(columnName, staffNum); } else if (columnName.equals("yearRevenue")) { double yearRevenue = customer.getYearRevenue(); dynamicValue.put(columnName, TextUtil.doubleToText(yearRevenue, 2)); } else if (columnName.equals("strField1")) { dynamicValue.put(columnName, customer.getStrField1()); } else if (columnName.equals("strField2")) { dynamicValue.put(columnName, customer.getStrField2()); } else if (columnName.equals("strField3")) { dynamicValue.put(columnName, customer.getStrField3()); } else if (columnName.equals("strField4")) { dynamicValue.put(columnName, customer.getStrField4()); } else if (columnName.equals("strField5")) { dynamicValue.put(columnName, customer.getStrField5()); } else if (columnName.equals("strField6")) { dynamicValue.put(columnName, customer.getStrField6()); } else if (columnName.equals("strField7")) { dynamicValue.put(columnName, customer.getStrField7()); } else if (columnName.equals("strField8")) { dynamicValue.put(columnName, customer.getStrField8()); } else if (columnName.equals("strField9")) { dynamicValue.put(columnName, customer.getStrField9()); } else if (columnName.equals("strField10")) { dynamicValue.put(columnName, customer.getStrField10()); } else if (columnName.equals("textareaField1")) { String textareaField1 = customer.getTextareaField1(); if (textareaField1 == null) { textareaField1 = StringPool.BLANK; } dynamicValue.put(columnName, textareaField1); } else if (columnName.equals("textareaField2")) { String textareaField2 = customer.getTextareaField2(); if (textareaField2 == null) { textareaField2 = StringPool.BLANK; } dynamicValue.put(columnName, textareaField2); } else if (columnName.equals("textareaField3")) { String textareaField3 = customer.getTextareaField3(); if (textareaField3 == null) { textareaField3 = StringPool.BLANK; } dynamicValue.put(columnName, textareaField3); } else if (columnName.equals("textareaField4")) { String textareaField4 = customer.getTextareaField4(); if (textareaField4 == null) { textareaField4 = StringPool.BLANK; } dynamicValue.put(columnName, textareaField4); } else if (columnName.equals("floatField1")) { double floatField1 = customer.getFloatField1(); dynamicValue.put(columnName, TextUtil.doubleToText(floatField1, 2)); } else if (columnName.equals("floatField2")){ double floatField2 = customer.getFloatField2(); dynamicValue.put(columnName, TextUtil.doubleToText(floatField2, 2)); } else if (columnName.equals("floatField3")){ double floatField3 = customer.getFloatField3(); dynamicValue.put(columnName, TextUtil.doubleToText(floatField3, 2)); } else if (columnName.equals("floatField4")){ double floatField4 = customer.getFloatField4(); dynamicValue.put(columnName, TextUtil.doubleToText(floatField4, 2)); } else if (columnName.equals("dateField1")){ dynamicValue.put(columnName, DateUtil.formatDateNoHour(customer.getDateField1())); } else if (columnName.equals("dateField2")){ dynamicValue.put(columnName, DateUtil.formatDateNoHour(customer.getDateField2())); } else if (columnName.equals("dateField3")){ dynamicValue.put(columnName, DateUtil.formatDateNoHour(customer.getDateField3())); } else if (columnName.equals("dateField4")){ dynamicValue.put(columnName, DateUtil.formatDateNoHour(customer.getDateField4())); } else if (columnName.equals("attaField1")) { dynamicValue.put(columnName, getAttathment(portletRequest, customer.getAttaField1())); } else if (columnName.equals("attaField2")) { dynamicValue.put(columnName, getAttathment(portletRequest, customer.getAttaField2())); } else if (columnName.equals("integerField1")) { dynamicValue.put(columnName, String.valueOf(customer.getIntegerField1())); } else if (columnName.equals("integerField2")) { dynamicValue.put(columnName, String.valueOf(customer.getIntegerField2())); } else if (columnName.equals("integerField3")) { dynamicValue.put(columnName, String.valueOf(customer.getIntegerField3())); } else if (columnName.equals("integerField4")) { dynamicValue.put(columnName, String.valueOf(customer.getIntegerField4())); } } } } return dynamicValue; }
在上述的源代码中:
List<CompanyColumnDefinition> companyColumns = CompanyColumnDefinitionLocalServiceUtil.searchByTableName(companyId, TableConst.CRM_Customer, true);
这一句话是不是看起来很简单,只看名字估计大家都能知道什么意思,不就是通过数据库的名字找到数据库中的所有的列吗?大家用脚指头想想,也是这个意思,第一个参数代表是公司的id。第二个参数表的名字。是你真聪明,但是后台他做的事情很多的。这个等我看过源代码再给大家补充上去,我只是猜测,不敢断言。通过注解可以实现通过数据库的表的名字能找到列的名字。
if (columnName.equals("dateField1")){ dynamicValue.put(columnName, DateUtil.formatDateNoHour(customer.getDateField1()));
}这一行代码的意思是不是解释了大家的疑问。它是没有改变列的名字,只是修改了列的对应的内容。