Skip to content

Commit 0e39c92

Browse files
committed
#558 When an entity's title changes, update its filesystem path
1 parent 35ae77b commit 0e39c92

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

quick-start/src/main/java/com/marklogic/quickstart/service/EntityManagerService.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,41 @@ public EntityModel createEntity(String projectDir, EntityModel newEntity) throws
156156
public EntityModel saveEntity(EntityModel entity) throws IOException {
157157
JsonNode node = entity.toJson();
158158
ObjectMapper objectMapper = new ObjectMapper();
159-
String filename = entity.getFilename();
160-
if (filename == null) {
161-
String title = entity.getInfo().getTitle();
159+
String fullpath = entity.getFilename();
160+
String title = entity.getInfo().getTitle();
161+
162+
if (fullpath == null) {
162163
Path dir = Paths.get(envConfig().getProjectDir(), PLUGINS_DIR, ENTITIES_DIR, title);
163164
if (!dir.toFile().exists()) {
164165
dir.toFile().mkdirs();
165166
}
166-
filename = Paths.get(dir.toString(), title + ENTITY_FILE_EXTENSION).toString();
167+
fullpath = Paths.get(dir.toString(), title + ENTITY_FILE_EXTENSION).toString();
167168
}
169+
else {
170+
String filename = new File(fullpath).getName();
171+
String entityFromFilename = filename.substring(0, filename.indexOf(ENTITY_FILE_EXTENSION));
172+
if (!entityFromFilename.equals(entity.getName())) {
173+
// The entity name was changed since the files were created. Update
174+
// the path.
175+
176+
// Update the name of the entity definition file
177+
File origFile = new File(fullpath);
178+
File newFile = new File(origFile.getParent() + File.separator + title + ENTITY_FILE_EXTENSION);
179+
origFile.renameTo(newFile);
180+
181+
// Update the directory name
182+
File origDirectory = new File(origFile.getParent());
183+
File newDirectory = new File(origDirectory.getParent() + File.separator + title);
184+
origDirectory.renameTo(newDirectory);
185+
186+
fullpath = newDirectory.getAbsolutePath() + File.separator + title + ENTITY_FILE_EXTENSION;
187+
entity.setFilename(fullpath);
188+
}
189+
}
190+
168191

169192
String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
170-
FileUtils.writeStringToFile(new File(filename), json);
193+
FileUtils.writeStringToFile(new File(fullpath), json);
171194

172195
return entity;
173196
}

quick-start/src/test/java/com/marklogic/quickstart/service/EntityManagerServiceTest.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.marklogic.quickstart.service;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import com.marklogic.hub.HubConfig;
46
import com.marklogic.hub.HubTestBase;
57
import com.marklogic.hub.flow.CodeFormat;
@@ -31,6 +33,7 @@
3133
public class EntityManagerServiceTest extends HubTestBase {
3234

3335
private static String ENTITY = "test-entity";
36+
private static String ENTITY2 = "test-entity2";
3437
private static Path projectDir = Paths.get(".", PROJECT_PATH);
3538

3639
@Autowired
@@ -112,7 +115,23 @@ public void getEntities() throws IOException {
112115
}
113116

114117
@Test
115-
public void saveEntity() {
118+
public void saveEntity() throws IOException {
119+
Path entityDir = projectDir.resolve("plugins/entities/" + ENTITY);
120+
String entityFilename = ENTITY2 + EntityManagerService.ENTITY_FILE_EXTENSION;
121+
122+
JsonNode node = getJsonFromResource(entityFilename);
123+
124+
EntityModel entity = EntityModel.fromJson(entityFilename, node);
125+
entity.setFilename(entityDir.resolve(entityFilename).toString());
126+
127+
entityMgrService.saveEntity(entity);
128+
129+
List<EntityModel> entities = entityMgrService.getEntities();
130+
131+
Assert.assertEquals(2, entities.size());
132+
String[] expected = {ENTITY, ENTITY2};
133+
String[] actual = { entities.get(0).getName(), entities.get(1).getName() };
134+
Assert.assertArrayEquals(expected, actual);
116135
}
117136

118137
@Test
@@ -157,4 +176,39 @@ public void getFlowByWrongType() throws IOException {
157176
Assert.assertNull(flow);
158177
}
159178

179+
/**
180+
* Addresses https://github.com/marklogic-community/marklogic-data-hub/issues/558.
181+
*/
182+
@Test
183+
public void changeEntityName() throws IOException {
184+
final String RENAMED_ENTITY = "renamed-entity";
185+
186+
// Get the original entity
187+
EntityModel entity = entityMgrService.getEntity(ENTITY);
188+
189+
// Convert to String and change the title (the UI just changes the title property)
190+
String strEntity = entity.toJson().toString();
191+
strEntity = strEntity.replaceFirst("\"title\"\\s*:\\s*\"test-entity\"", "\"title\" : \"" + RENAMED_ENTITY + "\"");
192+
strEntity = strEntity.replaceFirst("\"test-entity\"\\s*:", "\"" + RENAMED_ENTITY + "\" :");
193+
194+
// Convert back to JsonNode
195+
ObjectMapper mapper = new ObjectMapper();
196+
JsonNode renamed = mapper.readTree(strEntity);
197+
EntityModel renamedEntity = EntityModel.fromJson(entity.getFilename(), renamed);
198+
199+
// Save the renamedEntity
200+
entityMgrService.saveEntity(renamedEntity);
201+
202+
List<EntityModel> entities = entityMgrService.getEntities();
203+
Assert.assertEquals(1, entities.size());
204+
205+
// TODO: try to load the flows, which will fail.
206+
final String FLOW_NAME = "sjs-json-input-flow";
207+
List<FlowModel> inputFlows = entities.get(0).getInputFlows();
208+
209+
Assert.assertEquals(RENAMED_ENTITY, inputFlows.get(0).entityName);
210+
Assert.assertEquals(FLOW_NAME, inputFlows.get(0).flowName);
211+
Assert.assertEquals(FlowType.INPUT, inputFlows.get(0).flowType);
212+
213+
}
160214
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"info" : {
3+
"title" : "test-entity2",
4+
"version" : "0.0.1"
5+
},
6+
"definitions" : {
7+
"test-entity2" : {
8+
"required" : [ ],
9+
"rangeIndex" : [ ],
10+
"wordLexicon" : [ ],
11+
"properties" : {
12+
}
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)