Skip to content

Commit 3a83964

Browse files
author
Vladimir Kotal
committed
generate DTO as Java bean
1 parent 7345e85 commit 3a83964

File tree

4 files changed

+44
-108
lines changed

4 files changed

+44
-108
lines changed

opengrok-indexer/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ Portions Copyright (c) 2017-2019, Chris Fraire <[email protected]>.
194194
<artifactId>jackson-annotations</artifactId>
195195
<version>${jackson.version}</version>
196196
</dependency>
197+
<dependency>
198+
<groupId>cglib</groupId>
199+
<artifactId>cglib</artifactId>
200+
<version>3.2.4</version>
201+
</dependency>
197202
</dependencies>
198203

199204
<build>

opengrok-indexer/src/main/java/org/opengrok/indexer/history/RepositoryInfo.java

Lines changed: 32 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.Objects;
3131
import java.util.logging.Level;
3232
import java.util.logging.Logger;
33+
34+
import net.sf.cglib.beans.BeanGenerator;
3335
import org.opengrok.indexer.configuration.Project;
3436
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3537
import org.opengrok.indexer.logger.LoggerFactory;
@@ -68,93 +70,6 @@ public class RepositoryInfo implements Serializable {
6870
private boolean handleRenamedFiles;
6971
private boolean historyEnabled;
7072

71-
public static class RepositoryInfoTO implements Serializable {
72-
private static final long serialVersionUID = -1;
73-
74-
// same members as in RepositoryInfo except datePatterns
75-
private String directoryNameRelative;
76-
private boolean working;
77-
private String type;
78-
private boolean remote;
79-
private String parent;
80-
private String branch;
81-
private String currentVersion;
82-
private boolean handleRenamedFiles;
83-
private boolean historyEnabled;
84-
85-
public String getDirectoryNameRelative() {
86-
return directoryNameRelative;
87-
}
88-
89-
public void setDirectoryNameRelative(String directoryNameRelative) {
90-
this.directoryNameRelative = directoryNameRelative;
91-
}
92-
93-
public boolean isWorking() {
94-
return working;
95-
}
96-
97-
public void setWorking(boolean working) {
98-
this.working = working;
99-
}
100-
101-
public String getType() {
102-
return type;
103-
}
104-
105-
public void setType(String type) {
106-
this.type = type;
107-
}
108-
109-
public boolean isRemote() {
110-
return remote;
111-
}
112-
113-
public void setRemote(boolean remote) {
114-
this.remote = remote;
115-
}
116-
117-
public String getParent() {
118-
return parent;
119-
}
120-
121-
public void setParent(String parent) {
122-
this.parent = parent;
123-
}
124-
125-
public String getBranch() {
126-
return branch;
127-
}
128-
129-
public void setBranch(String branch) {
130-
this.branch = branch;
131-
}
132-
133-
public String getCurrentVersion() {
134-
return currentVersion;
135-
}
136-
137-
public void setCurrentVersion(String currentVersion) {
138-
this.currentVersion = currentVersion;
139-
}
140-
141-
public boolean isHandleRenamedFiles() {
142-
return handleRenamedFiles;
143-
}
144-
145-
public void setHandleRenamedFiles(boolean handleRenamedFiles) {
146-
this.handleRenamedFiles = handleRenamedFiles;
147-
}
148-
149-
public boolean isHistoryEnabled() {
150-
return historyEnabled;
151-
}
152-
153-
public void setHistoryEnabled(boolean historyEnabled) {
154-
this.historyEnabled = historyEnabled;
155-
}
156-
}
157-
15873
/**
15974
* Empty constructor to support serialization.
16075
*/
@@ -174,31 +89,42 @@ public RepositoryInfo(RepositoryInfo orig) {
17489
}
17590

17691
/**
177-
* @return Data Transfer Object for RepositoryInfo
92+
* @return Data Transfer Object for RepositoryInfo.
93+
* It will have the same members as in RepositoryInfo except datePatterns.
17894
*/
179-
public RepositoryInfoTO getRepositoryInfoData() {
95+
public Object getRepositoryInfoData() {
18096
return createRepositoryInfoTO();
18197
}
18298

183-
private RepositoryInfoTO createRepositoryInfoTO() {
184-
RepositoryInfoTO ri = new RepositoryInfoTO();
99+
private Object createRepositoryInfoTO() {
100+
BeanGenerator beanGenerator = new BeanGenerator();
101+
beanGenerator.addProperty("type", String.class);
102+
beanGenerator.addProperty("directoryNameRelative", String.class);
103+
beanGenerator.addProperty("remote", boolean.class);
104+
beanGenerator.addProperty("parent", String.class);
105+
beanGenerator.addProperty("branch", String.class);
106+
beanGenerator.addProperty("currentVersion", String.class);
107+
beanGenerator.addProperty("working", Boolean.class);
108+
beanGenerator.addProperty("handleRenamedFiles", boolean.class);
109+
beanGenerator.addProperty("historyEnabled", boolean.class);
185110

186-
if (this.working == null) {
187-
ri.working = false;
188-
} else {
189-
ri.working = this.working;
111+
Object myBean = beanGenerator.create();
112+
try {
113+
ClassUtil.setFieldValue(myBean, "type", this.type);
114+
ClassUtil.setFieldValue(myBean, "working",
115+
this.working == null ? false : this.working);
116+
ClassUtil.setFieldValue(myBean, "directoryNameRelative", this.directoryNameRelative);
117+
ClassUtil.setFieldValue(myBean, "remote", this.remote);
118+
ClassUtil.setFieldValue(myBean, "parent", this.parent);
119+
ClassUtil.setFieldValue(myBean, "branch", this.branch);
120+
ClassUtil.setFieldValue(myBean, "currentVersion", this.currentVersion);
121+
ClassUtil.setFieldValue(myBean, "handleRenamedFiles", this.handleRenamedFiles);
122+
ClassUtil.setFieldValue(myBean, "historyEnabled", this.historyEnabled);
123+
} catch (IOException e) {
124+
LOGGER.log(Level.WARNING, "cannot generate RepositoryInfo bean", e);
125+
return null;
190126
}
191-
192-
ri.directoryNameRelative = this.directoryNameRelative;
193-
ri.type = this.type;
194-
ri.remote = this.remote;
195-
ri.parent = this.parent;
196-
ri.branch = this.branch;
197-
ri.currentVersion = this.currentVersion;
198-
ri.handleRenamedFiles = this.handleRenamedFiles;
199-
ri.historyEnabled = this.historyEnabled;
200-
201-
return ri;
127+
return myBean;
202128
}
203129

204130
/**

opengrok-web/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
148148
<artifactId>org.suigeneris.jrcs.diff</artifactId>
149149
<version>0.4.2</version>
150150
</dependency>
151+
<dependency>
152+
<groupId>cglib</groupId>
153+
<artifactId>cglib</artifactId>
154+
<version>3.2.4</version>
155+
</dependency>
151156
</dependencies>
152157

153158
<build>

opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/RepositoriesController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class RepositoriesController {
4141

4242
private RuntimeEnvironment env = RuntimeEnvironment.getInstance();
4343

44-
private RepositoryInfo.RepositoryInfoTO getRepositoryInfoData(String repositoryPath) {
44+
private Object getRepositoryInfoData(String repositoryPath) {
4545
for (RepositoryInfo ri : env.getRepositories()) {
4646
if (ri.getDirectoryNameRelative().equals(repositoryPath)) {
4747
return ri.getRepositoryInfoData();
@@ -57,7 +57,7 @@ private RepositoryInfo.RepositoryInfoTO getRepositoryInfoData(String repositoryP
5757
public Object get(@QueryParam("repository") final String repositoryPath, @PathParam("field") final String field)
5858
throws IOException {
5959

60-
RepositoryInfo.RepositoryInfoTO ri = getRepositoryInfoData(repositoryPath);
60+
Object ri = getRepositoryInfoData(repositoryPath);
6161
if (ri == null) {
6262
throw new WebApplicationException("cannot find repository with path: " + repositoryPath,
6363
Response.Status.NOT_FOUND);

0 commit comments

Comments
 (0)