Skip to content

Commit

Permalink
增加主要的管理功能
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-lee committed Oct 8, 2015
1 parent 0c7ef83 commit dbfc469
Show file tree
Hide file tree
Showing 28 changed files with 4,689 additions and 6 deletions.
5 changes: 5 additions & 0 deletions config-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
Expand Down
238 changes: 238 additions & 0 deletions config-web/src/main/java/com/github/autoconf/entity/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
package com.github.autoconf.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.html.HtmlEscapers;
import org.hibernate.validator.constraints.Length;
import org.joda.time.DateTime;

import javax.persistence.Transient;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 配置对象
* <p/>
* Created by lirui on 15/1/10.
*/
public class Config {
@Transient
protected Function<String, String> escape = new Function<String, String>() {
@Override
public String apply(String input) {
return HtmlEscapers.htmlEscaper().escape(input);
}
};

private Long id;
/**
* 配置文件名
*/
@Length(min = 2, max = 64)
private String name;
/**
* 配置文件路径
*/
private String path;
/**
* 配置组信息
*/
@Length(min = 2, max = 128)
private String profile;
/**
* 版本号
*/
private int version;
/**
* 配置内容(摘要)
*/
@Length(min = 1)
private String content;
/**
* 谁修改了配置
*/
private String editor;
/**
* 最后修改时间
*/
@Transient
private Date modifyTime;
/**
* 保存到zookeeper上需要使用的编码方式
*/
private String encoding;
/**
* 查找的时候给出摘要信息
*/
@Transient
private String summary;

public Config() {
}

public Config(String name, String profile, String content) {
this.content = content;
setName(name);
setProfile(profile);
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
updatePath();
}

@JsonIgnore
public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getProfile() {
return profile;
}

public void setProfile(String profile) {
this.profile = profile;
updatePath();
}

//TODO: XXXX
private void updatePath() {
this.path = "/cms/" + name + "/" + profile;
}

public int getVersion() {
return version;
}

public void setVersion(int version) {
this.version = version;
}

public void incVersion() {
this.version++;
}

@JsonIgnore
public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getEditor() {
return editor;
}

public void setEditor(String editor) {
this.editor = editor;
}

@JsonIgnore
public Date getModifyTime() {
return modifyTime;
}

public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}

public String getMtime() {
return new DateTime(modifyTime).toString("yyyy-MM-dd HH:mm:ss");
}

public String getEncoding() {
return encoding;
}

public void setEncoding(String encoding) {
this.encoding = encoding;
}

public String getSummary() {
if (summary == null) {
List<String> lines = Splitter.on('\n').trimResults().omitEmptyStrings().splitToList(content);
List<String> items = lines.subList(0, Math.min(10, lines.size()));
summary = Joiner.on("<br/>").join(Iterables.transform(items, escape));
}
return summary;
}

public void setSummary(List<String> s) {
if (content != null) {
List<String> lines = Splitter.on('\n').trimResults().omitEmptyStrings().splitToList(content);
List<String> head = Lists.newArrayList();
if (!Iterables.isEmpty(s)) {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (String i : s) {
sb.append(CharMatcher.is('|').replaceFrom(i, "\\|")).append('|');
}
sb.setCharAt(sb.length() - 1, ')');
Pattern p = Pattern.compile(sb.toString(), Pattern.CASE_INSENSITIVE);
int count = 0;
for (String i : lines) {
String n = highlight(i, p);
if (n.length() > i.length()) {
head.add(n);
if (++count > 10)
break;
}
}
if (head.size() > 0) {
summary = Joiner.on("<br/>").join(head);
}
} else {
summary = null;
}
}
}

protected String highlight(String s, Pattern p) {
Matcher m = p.matcher(s);
StringBuilder sb = new StringBuilder();
int start = 0;
while (m.find()) {
sb.append(HtmlEscapers.htmlEscaper().escape(s.substring(start, m.start(1))));
sb.append("<span class=\"bg-danger text-blue\">").append(m.group(1)).append("</span>");
start = m.end(1);
}
if (start < s.length()) {
sb.append(HtmlEscapers.htmlEscaper().escape(s.substring(start)));
}
return sb.toString();
}

public boolean isNew() {
return id == null;
}

@Override
public String toString() {
return "Config{" + "name=" + name + ',' + "profile=" + profile + ',' + "version=" + version + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.autoconf.entity;

/**
* 配置对象
* <p/>
* Created by lirui on 15/1/10.
*/
public class ConfigHistory extends Config {
private long configId;

public long getConfigId() {
return configId;
}

public void setConfigId(long configId) {
this.configId = configId;
}

public void copy(Config old) {
this.setVersion(old.getVersion());
this.setName(old.getName());
this.setProfile(old.getProfile());
this.setPath(old.getPath());
this.setContent(old.getContent());
this.setEditor(old.getEditor());
this.setModifyTime(old.getModifyTime());
this.setConfigId(old.getId());
}

@Override
public String toString() {
return "ConfigHistory{" + "name=" + getName() + ',' + "profile=" + getProfile() + ',' + "version=" + getVersion() + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.github.autoconf.entity;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Set;

/**
* 替换配置内容的请求
* <p/>
* Created by lirui on 2015/03/02 上午10:44.
*/
public class ReplaceRequest implements Serializable {
@NotNull
private String src;
@NotNull
private String dst;
private Set<Long> configIds;
private Config config;
private String oldLines;
private String newLines;

public String getSrc() {
return src;
}

public void setSrc(String src) {
this.src = src;
}

public String getDst() {
return dst;
}

public void setDst(String dst) {
this.dst = dst;
}

public Config getConfig() {
return config;
}

public void setConfig(Config config) {
this.config = config;
}

public Set<Long> getConfigIds() {
return configIds;
}

public void setConfigIds(Set<Long> configIds) {
this.configIds = configIds;
}

public String getOldLines() {
return oldLines;
}

public void setOldLines(String oldLines) {
this.oldLines = oldLines;
}

public String getNewLines() {
return newLines;
}

public void setNewLines(String newLines) {
this.newLines = newLines;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ReplaceRequest{");
sb.append("src='").append(src).append('\'');
sb.append(", dst='").append(dst).append('\'');
sb.append('}');
return sb.toString();
}
}
Loading

0 comments on commit dbfc469

Please sign in to comment.