Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemnting Patterns of Desing: #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 62 additions & 47 deletions src/main/java/com/vincit/go/task/slack/config/ConfigProvider.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,62 @@
package com.vincit.go.task.slack.config;

import com.vincit.go.task.slack.model.ChannelType;
import com.vincit.go.task.slack.model.ColorType;
import com.vincit.go.task.slack.utils.Required;
import com.vincit.go.task.slack.utils.Secure;

import java.util.HashMap;
import java.util.Map;

import static com.vincit.go.task.slack.config.FieldUtils.createField;

public class ConfigProvider {

public static final String CHANNEL = "Channel";
public static final String CHANNEL_TYPE = "ChannelType";
public static final String MESSAGE = "Message";
public static final String TITLE = "Title";
public static final String ICON_OR_EMOJI = "IconOrEmoji";
public static final String WEBHOOK_URL = "WebhookUrl";
public static final String DISPLAY_NAME = "DisplayName";
public static final String COLOR = "Color";
public static final String COLOR_TYPE = "ColorType";
public static final String MARKDOWN_IN_TEXT = "MarkdownInText";
public static final String FAIL_ON_ERROR = "FailOnError";

private static Map<String, HashMap<String, Object>> fieldConfig = new HashMap<>();

static {
fieldConfig.put(WEBHOOK_URL, createField("", Secure.NO, Required.YES));
fieldConfig.put(CHANNEL, createField("", Secure.NO, Required.YES));
fieldConfig.put(CHANNEL_TYPE, createField(ChannelType.CHANNEL.getDisplayValue(), Secure.NO, Required.YES));
fieldConfig.put(TITLE, createField("", Secure.NO, Required.NO));
fieldConfig.put(ICON_OR_EMOJI, createField("", Secure.NO, Required.NO));
fieldConfig.put(MESSAGE, createField("", Secure.NO, Required.NO));
fieldConfig.put(DISPLAY_NAME, createField("", Secure.NO, Required.NO));
fieldConfig.put(COLOR_TYPE, createField(ColorType.NONE.getDisplayValue(), Secure.NO, Required.YES));
fieldConfig.put(COLOR, createField("", Secure.NO, Required.NO));
fieldConfig.put(MARKDOWN_IN_TEXT, createField("", Secure.NO, Required.NO));
fieldConfig.put(FAIL_ON_ERROR, createField("", Secure.NO, Required.NO));
}

public static Map<String, ?> getFieldConfig() {
return fieldConfig;
}

}
package com.vincit.go.task.slack.config;

import com.vincit.go.task.slack.model.ChannelType;
import com.vincit.go.task.slack.model.ColorType;
import com.vincit.go.task.slack.utils.Required;
import com.vincit.go.task.slack.utils.Secure;

import java.util.HashMap;
import java.util.Map;

import static com.vincit.go.task.slack.config.FieldUtils.createField;

public class ConfigProvider {
private static ConfigProvider instance;
public static final String CHANNEL = "Channel";
public static final String CHANNEL_TYPE = "ChannelType";
public static final String MESSAGE = "Message";
public static final String TITLE = "Title";
public static final String ICON_OR_EMOJI = "IconOrEmoji";
public static final String WEBHOOK_URL = "WebhookUrl";
public static final String DISPLAY_NAME = "DisplayName";
public static final String COLOR = "Color";
public static final String COLOR_TYPE = "ColorType";
public static final String MARKDOWN_IN_TEXT = "MarkdownInText";
public static final String FAIL_ON_ERROR = "FailOnError";

private static Map<String, HashMap<String, Object>> fieldConfig = new HashMap<>();

static {
fieldConfig.put(WEBHOOK_URL, createField("", Secure.NO, Required.YES));
fieldConfig.put(CHANNEL, createField("", Secure.NO, Required.YES));
fieldConfig.put(CHANNEL_TYPE, createField(ChannelType.CHANNEL.getDisplayValue(), Secure.NO, Required.YES));
fieldConfig.put(TITLE, createField("", Secure.NO, Required.NO));
fieldConfig.put(ICON_OR_EMOJI, createField("", Secure.NO, Required.NO));
fieldConfig.put(MESSAGE, createField("", Secure.NO, Required.NO));
fieldConfig.put(DISPLAY_NAME, createField("", Secure.NO, Required.NO));
fieldConfig.put(COLOR_TYPE, createField(ColorType.NONE.getDisplayValue(), Secure.NO, Required.YES));
fieldConfig.put(COLOR, createField("", Secure.NO, Required.NO));
fieldConfig.put(MARKDOWN_IN_TEXT, createField("", Secure.NO, Required.NO));
fieldConfig.put(FAIL_ON_ERROR, createField("", Secure.NO, Required.NO));
}



private ConfigProvider(){

}

public static ConfigProvider getInstance(){
if(instance==null){
instance=new ConfigProvider();
}
return instance;
}



public static Map<String, ?> getFieldConfig() {
return fieldConfig;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.vincit.go.task.slack.executor;

import java.io.IOException;

/**
*
* @author alexc
*/
public class SlackExecutorComponent implements SlackExecutorInterface{

@Override
public void sendMessage(TaskSlackMessage message) throws IOException {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.vincit.go.task.slack.executor;

import java.io.IOException;

/**
*
* @author alexc
*/
public class SlackExecutorDecorator implements SlackExecutorInterface{


private SlackExecutorInterface component;

public SlackExecutorDecorator(SlackExecutorInterface component){
this.component=component;
}
@Override
public void sendMessage(TaskSlackMessage message) throws IOException {
component.sendMessage(message);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.vincit.go.task.slack.executor;

public class SlackExecutorFactory_1 {

public SlackExecutor forDestination(TaskSlackDestination destination) {
return new SlackExecutor(destination);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package com.vincit.go.task.slack.executor;

import java.io.IOException;

/**
*
* @author alexc
*/
public interface SlackExecutorInterface {
void sendMessage(TaskSlackMessage message) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.vincit.go.task.slack.executor;

import java.io.IOException;

/**
*
* @author alexc
*/

public class SlackExecutorLogginDecorator extends SlackExecutorDecorator{
private SlackExecutorDecorator component;

public SlackExecutorLogginDecorator(SlackExecutorInterface component) {
super(component);
}



@Override
public void sendMessage(TaskSlackMessage message) throws IOException {
component.sendMessage(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.vincit.go.task.slack.executor;

import java.io.IOException;

/**
*
* @author alexc
*/
public class SlackExecutorTimingDecorator extends SlackExecutorDecorator{
private SlackExecutorInterface component;
public SlackExecutorTimingDecorator(SlackExecutorInterface component) {
super(component);
}
@Override
public void sendMessage(TaskSlackMessage message) throws IOException {
long startTime=System.currentTimeMillis();
super.sendMessage(message);
long endTime=System.currentTimeMillis();
System.out.println("Tiempo de Ejecución de SlackExecutor"+(endTime - startTime)+ "ms");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.vincit.go.task.slack.executor;

import com.vincit.go.task.slack.model.ChannelType;
import com.vincit.go.task.slack.model.MarkdownField;
import in.ashwanthkumar.slack.webhook.Slack;
import in.ashwanthkumar.slack.webhook.SlackAttachment;

import java.io.IOException;

public class SlackExecutor_1 implements SlackExecutorInterface{

private Slack slack;
private TaskSlackDestination destination;

SlackExecutor_1(Slack slack, TaskSlackDestination destination) {
this.slack = slack;
this.destination = destination;
}

SlackExecutor_1(TaskSlackDestination destination) {
this.destination = destination;
this.slack = new Slack(destination.getWebhookUrl());
}
SlackExecutor_1(){

}

@Override
public void sendMessage(TaskSlackMessage message) throws IOException {
SlackAttachment attachment = new SlackAttachment(message.getMessage())
.color(message.getColor())
.title(message.getTitle());

for (MarkdownField field : message.getMarkdownIns()) {
attachment.addMarkdownIn(field.getApiValue());
}

updateChannel(destination);

slack.displayName(message.getDisplayName());
slack.icon(message.getIconOrEmoji());

slack.push(attachment);
}

private void updateChannel(TaskSlackDestination destination) {
if (destination.getChannelType() == ChannelType.CHANNEL) {
slack.sendToChannel(destination.getChannel());
} else {
slack.sendToUser(destination.getChannel());
}
}
}
Loading