1 package org.kuali.common.devops.jenkins.upgrade.model;
2
3 import static com.google.common.collect.Lists.newArrayList;
4 import static org.kuali.common.core.io.Files.checkIsDir;
5 import static org.kuali.common.core.io.Files.getRegularFiles;
6 import static org.kuali.common.core.io.Files.walkFileTree;
7 import static org.kuali.common.devops.jenkins.upgrade.Jenkins.parseBuildStartTime;
8 import static org.kuali.common.util.base.Precondition.checkMin;
9 import static org.kuali.common.util.base.Precondition.checkNotNull;
10 import static org.kuali.common.util.tree.Trees.breadthFirstElements;
11
12 import java.io.IOException;
13 import java.util.List;
14
15 import org.kuali.common.core.io.UnixFile;
16 import org.kuali.common.util.tree.Node;
17
18 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
19 import com.google.common.collect.ImmutableList;
20
21 @JsonDeserialize(builder = BuildSummary.Builder.class)
22 public final class BuildSummary {
23
24 public static final String BUILD_DIR_DATE_FORMAT = "yyyy-MM-dd_HH-mm-ss";
25
26 private final long startTime;
27 private final UnixFile basedir;
28 private final ImmutableList<UnixFile> files;
29 private final int buildNumber;
30
31 private BuildSummary(Builder builder) {
32 this.basedir = builder.basedir;
33 this.startTime = builder.startTime;
34 this.buildNumber = builder.buildNumber;
35 this.files = ImmutableList.copyOf(builder.files);
36 }
37
38 public static BuildSummary copyOf(BuildSummary build, List<UnixFile> files) {
39 Builder builder = builder();
40 builder.withBasedir(build.getBasedir());
41 builder.withStartTime(build.getStartTime());
42 builder.withBuildNumber(build.getBuildNumber());
43 builder.withFiles(files);
44 return builder.build();
45 }
46
47 public static BuildSummary build(UnixFile basedir, int buildNumber, String timeZoneId) throws IOException {
48 checkIsDir(basedir);
49 long startTime = parseBuildStartTime(basedir, timeZoneId);
50 Node<UnixFile> tree = walkFileTree(basedir);
51 List<UnixFile> files = getRegularFiles(breadthFirstElements(tree));
52 return builder().withBasedir(basedir).withStartTime(startTime).withBuildNumber(buildNumber).withFiles(files).build();
53 }
54
55 public static Builder builder() {
56 return new Builder();
57 }
58
59 public static class Builder implements org.apache.commons.lang3.builder.Builder<BuildSummary> {
60
61 private long startTime;
62 private UnixFile basedir;
63 private List<UnixFile> files = newArrayList();
64 private int buildNumber = -1;
65
66 public Builder withBuildNumber(int buildNumber) {
67 this.buildNumber = buildNumber;
68 return this;
69 }
70
71 public Builder withFiles(List<UnixFile> files) {
72 this.files = files;
73 return this;
74 }
75
76 public Builder withStartTime(long start) {
77 this.startTime = start;
78 return this;
79 }
80
81 public Builder withBasedir(UnixFile basedir) {
82 this.basedir = basedir;
83 return this;
84 }
85
86 @Override
87 public BuildSummary build() {
88 return validate(new BuildSummary(this));
89 }
90
91 private static BuildSummary validate(BuildSummary instance) {
92 checkNotNull(instance.basedir, "basedir");
93 checkNotNull(instance.files, "files");
94 checkMin(instance.buildNumber, 1, "buildNumber");
95 return instance;
96 }
97 }
98
99 public long getStartTime() {
100 return startTime;
101 }
102
103 public UnixFile getBasedir() {
104 return basedir;
105 }
106
107 public List<UnixFile> getFiles() {
108 return files;
109 }
110
111 public int getBuildNumber() {
112 return buildNumber;
113 }
114
115 }