Technoboy- commented on a change in pull request #1713: Refactor alert template URL: https://github.com/apache/incubator-dolphinscheduler/pull/1713#discussion_r363077428
########## File path: dolphinscheduler-alert/src/main/java/org/apache/dolphinscheduler/alert/template/impl/DefaultHTMLTemplate.java ########## @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dolphinscheduler.alert.template.impl; + +import org.apache.dolphinscheduler.alert.exception.NotSupportOperatorException; +import org.apache.dolphinscheduler.alert.template.AlertTemplate; +import org.apache.dolphinscheduler.alert.utils.Constants; +import org.apache.dolphinscheduler.alert.utils.JSONUtils; +import org.apache.dolphinscheduler.alert.utils.MailUtils; +import org.apache.dolphinscheduler.common.enums.ShowType; +import org.apache.dolphinscheduler.common.utils.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; + +import static org.apache.dolphinscheduler.common.utils.Preconditions.*; + +/** + * the default html alert message template + */ +public class DefaultHTMLTemplate implements AlertTemplate { + + public static final Logger logger = LoggerFactory.getLogger(DefaultHTMLTemplate.class); + + @Override + public String getMessageFromTemplate(String content, ShowType showType,boolean showAll) { + + switch (showType){ + case TABLE: + return getTableTypeMessage(content,showAll); + case TEXT: + return getTextTypeMessage(content,showAll); + default: + logger.error("not support showType: {} in DefaultHTMLTemplate",showType); + throw new NotSupportOperatorException(String.format("not support showType: %s in DefaultHTMLTemplate",showType)); + } + } + + /** + * get alert message which type is TABLE + * @param content message content + * @param showAll weather to show all + * @return alert message + */ + private String getTableTypeMessage(String content,boolean showAll){ + + if (StringUtils.isNotEmpty(content)){ + List<LinkedHashMap> mapItemsList = JSONUtils.toList(content, LinkedHashMap.class); + + if(!showAll && mapItemsList.size() > Constants.NUMBER_1000){ + mapItemsList = mapItemsList.subList(0,Constants.NUMBER_1000); + } + + StringBuilder contents = new StringBuilder(200); + + boolean flag = true; + + String title = ""; + for (LinkedHashMap mapItems : mapItemsList){ + + Set<Map.Entry<String, Object>> entries = mapItems.entrySet(); + + Iterator<Map.Entry<String, Object>> iterator = entries.iterator(); + + StringBuilder t = new StringBuilder(Constants.TR); + StringBuilder cs = new StringBuilder(Constants.TR); + while (iterator.hasNext()){ + + Map.Entry<String, Object> entry = iterator.next(); + t.append(Constants.TH).append(entry.getKey()).append(Constants.TH_END); + cs.append(Constants.TD).append(String.valueOf(entry.getValue())).append(Constants.TD_END); + + } + t.append(Constants.TR_END); + cs.append(Constants.TR_END); + if (flag){ + title = t.toString(); + } + flag = false; + contents.append(cs); + } + + return getMessageFromHtmlTemplate(title,contents.toString()); + } + + return null; Review comment: "return content" is better here . even though it's the same ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
