Skip to content

Commit 5e6e87b

Browse files
committed
1226 Template management ability to clone a template
1 parent 0ebfd0b commit 5e6e87b

File tree

11 files changed

+169
-54
lines changed

11 files changed

+169
-54
lines changed

logicaldoc-cmis/src/main/java/com/logicaldoc/cmis/LDRepository.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,7 @@ private void compileDocumentOrVersionProperties(AbstractDocument doc, ObjectInfo
19321932
}
19331933

19341934
private void compileDocumentOrVersionExtAttrsProperties(AbstractDocument doc, String typeId, PropertiesImpl result,
1935-
Set<String> filter) {
1935+
Set<String> filter) throws PersistenceException {
19361936

19371937
Template template = getTemplate(doc);
19381938

@@ -1983,7 +1983,7 @@ private void compileDocumentOrVersionExtAttrsProperties(AbstractDocument doc, St
19831983
}
19841984
}
19851985

1986-
private Template getTemplate(AbstractDocument doc) {
1986+
private Template getTemplate(AbstractDocument doc) throws PersistenceException {
19871987
Template template = doc.getTemplate();
19881988
if (doc instanceof Version && ((Version) doc).getTemplateName() != null)
19891989
template = templateDao.findByName(((Version) doc).getTemplateName(), doc.getTenantId());
@@ -2056,8 +2056,10 @@ public List<ObjectData> getAllVersions(String objectId) {
20562056
* @param doc The document to update
20572057
* @param properties The properties to use
20582058
* @param create True if we are updating metadata for creating a new element
2059+
*
2060+
* @throws PersistenceException Error in the data layer
20592061
*/
2060-
private void updateDocumentMetadata(AbstractDocument doc, Properties properties, boolean create) {
2062+
private void updateDocumentMetadata(AbstractDocument doc, Properties properties, boolean create) throws PersistenceException {
20612063
log.debug("updateDocumentMetadata doc: {}", doc);
20622064
log.debug("updateDocumentMetadata properties: {}", properties);
20632065

@@ -2071,7 +2073,7 @@ private void updateDocumentMetadata(AbstractDocument doc, Properties properties,
20712073
}
20722074

20732075
private void updateDocumentMetadata(AbstractDocument doc, Properties properties, PropertyData<?> p, boolean create,
2074-
TypeDefinition type) {
2076+
TypeDefinition type) throws PersistenceException {
20752077
PropertyDefinition<?> propType = type.getPropertyDefinitions().get(p.getId());
20762078

20772079
// do we know that property?
@@ -2128,7 +2130,7 @@ private void updateDocumentTags(AbstractDocument doc, PropertyData<?> p) {
21282130
}
21292131
}
21302132

2131-
private void updateDocumentTemplate(AbstractDocument doc, PropertyData<?> p) {
2133+
private void updateDocumentTemplate(AbstractDocument doc, PropertyData<?> p) throws PersistenceException {
21322134
if (p.getFirstValue() == null) {
21332135
doc.setTemplate(null);
21342136
if (doc instanceof Document)
@@ -2143,7 +2145,7 @@ private void updateDocumentTemplate(AbstractDocument doc, PropertyData<?> p) {
21432145
}
21442146
}
21452147

2146-
private void updateDocumentExtendedAttribute(AbstractDocument doc, Properties properties, PropertyData<?> p) {
2148+
private void updateDocumentExtendedAttribute(AbstractDocument doc, Properties properties, PropertyData<?> p) throws PersistenceException {
21472149
// try to load the document template first
21482150
Template template = null;
21492151
PropertyData<?> tp = properties.getProperties().get(TypeManager.PROP_TEMPLATE);

logicaldoc-core/src/main/java/com/logicaldoc/core/automation/DocTool.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,13 @@ public String calculateNextVersion(String currentVersion, boolean major) {
879879
*/
880880
public Template findTemplateByName(String name, long tenantId) {
881881
TemplateDAO dao = (TemplateDAO) Context.get().getBean(TemplateDAO.class);
882-
return dao.findByName(name, tenantId);
882+
883+
try {
884+
return dao.findByName(name, tenantId);
885+
} catch (PersistenceException e) {
886+
log.error(e.getMessage(), e);
887+
return null;
888+
}
883889
}
884890

885891
/**

logicaldoc-core/src/main/java/com/logicaldoc/core/metadata/HibernateTemplateDAO.java

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.ArrayList;
77
import java.util.HashSet;
88
import java.util.List;
9+
import java.util.Map;
910
import java.util.Set;
1011
import java.util.stream.Collectors;
1112

@@ -61,25 +62,22 @@ public List<Template> findAll(long tenantId) {
6162
return findByWhere(" " + ENTITY + TENANT_ID_EQUAL + tenantId, ORDER_BY + ENTITY + ".name", null);
6263
} catch (PersistenceException e) {
6364
log.error(e.getMessage(), e);
64-
return new ArrayList<>();
65+
return new ArrayList<Template>();
6566
}
6667
}
6768

6869
@Override
69-
public Template findByName(String name, long tenantId) {
70+
public Template findByName(String name, long tenantId) throws PersistenceException {
7071
Template template = null;
7172

72-
try {
73-
List<Template> coll = findByWhere(
74-
ENTITY + ".name = '" + SqlUtil.doubleQuotes(name) + "' and " + ENTITY + TENANT_ID_EQUAL + tenantId,
75-
null, null);
76-
if (CollectionUtils.isNotEmpty(coll))
77-
template = coll.iterator().next();
78-
if (template != null && template.getDeleted() == 1)
79-
template = null;
80-
} catch (PersistenceException e) {
81-
log.error(e.getMessage(), e);
82-
}
73+
List<Template> coll = findByWhere(
74+
ENTITY + ".name = '" + SqlUtil.doubleQuotes(name) + "' and " + ENTITY + TENANT_ID_EQUAL + tenantId,
75+
null, null);
76+
if (CollectionUtils.isNotEmpty(coll))
77+
template = coll.iterator().next();
78+
if (template != null && template.getDeleted() == 1)
79+
template = null;
80+
8381
return template;
8482
}
8583

@@ -107,13 +105,13 @@ public void delete(long id, int code) throws PersistenceException {
107105

108106
@Override
109107
public void store(Template template) throws PersistenceException {
110-
boolean isNew=template.getId()==0L;
108+
boolean isNew = template.getId() == 0L;
111109
super.store(template);
112110

113-
if(isNew) {
111+
if (isNew) {
114112
flush();
115113
storeSecurityAsync(template);
116-
}else {
114+
} else {
117115
storeSecurity(template);
118116
}
119117
}
@@ -176,24 +174,14 @@ public int countFolders(long id) {
176174
}
177175

178176
@Override
179-
public int countDocs(long id) {
180-
try {
181-
return queryForInt("select count(*) from ld_document where ld_deleted=0 and ld_templateid=" + id);
182-
} catch (PersistenceException e) {
183-
log.error(e.getMessage(), e);
184-
return 0;
185-
}
177+
public int countDocs(long id) throws PersistenceException {
178+
return queryForInt("select count(*) from ld_document where ld_deleted=0 and ld_templateid=" + id);
186179
}
187180

188181
@Override
189-
public List<Template> findByType(int type, long tenantId) {
190-
try {
191-
return findByWhere(ENTITY + ".type =" + type + " and " + ENTITY + TENANT_ID_EQUAL + tenantId,
192-
ORDER_BY + ENTITY + ".name asc", null);
193-
} catch (PersistenceException e) {
194-
log.error(e.getMessage(), e);
195-
return new ArrayList<>();
196-
}
182+
public List<Template> findByType(int type, long tenantId) throws PersistenceException {
183+
return findByWhere(ENTITY + ".type =" + type + " and " + ENTITY + TENANT_ID_EQUAL + tenantId,
184+
ORDER_BY + ENTITY + ".name asc", null);
197185
}
198186

199187
@Override
@@ -213,8 +201,9 @@ public void initialize(Template template) {
213201
tg.setWrite(groupSet.getInt(2));
214202
template.getTemplateGroups().add(tg);
215203
}
216-
} catch (Exception t) {
217-
// Nothing to do
204+
} catch (Exception e) {
205+
if (log.isErrorEnabled())
206+
log.error(e.getMessage(), e);
218207
}
219208
}
220209

@@ -291,4 +280,23 @@ public Set<Permission> getEnabledPermissions(long templateId, long userId) {
291280

292281
return permissions;
293282
}
283+
284+
@Override
285+
public Template clone(long id, String cloneName) throws PersistenceException {
286+
Template originalTemplate = findById(id, true);
287+
initialize(originalTemplate);
288+
Template clonedTemplate = new Template();
289+
clonedTemplate.setName(cloneName);
290+
if (originalTemplate.getLabel() != null)
291+
clonedTemplate.setLabel(originalTemplate.getLabel() + "-Clone");
292+
clonedTemplate.setDescription(originalTemplate.getDescription());
293+
clonedTemplate.setReadonly(originalTemplate.getReadonly());
294+
clonedTemplate.setValidation(originalTemplate.getValidation());
295+
clonedTemplate.setAttributes(originalTemplate.getAttributes().entrySet().stream()
296+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
297+
store(clonedTemplate);
298+
jdbcUpdate("insert into ld_templategroup(ld_templateid, ld_groupid, ld_write) select "+clonedTemplate.getId()+", ld_groupid, ld_write from ld_templategroup where ld_templateid="+id);
299+
initialize(clonedTemplate);
300+
return clonedTemplate;
301+
}
294302
}

logicaldoc-core/src/main/java/com/logicaldoc/core/metadata/TemplateDAO.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44
import java.util.Set;
55

6+
import com.logicaldoc.core.PersistenceException;
67
import com.logicaldoc.core.PersistentObjectDAO;
78
import com.logicaldoc.core.security.Permission;
89

@@ -20,16 +21,32 @@ public interface TemplateDAO extends PersistentObjectDAO<Template> {
2021
* @param tenantId ID of the owning tenant
2122
*
2223
* @return Template with given name
24+
*
25+
* @throws PersistenceException Error in the data layer
26+
*/
27+
public Template findByName(String name, long tenantId) throws PersistenceException;
28+
29+
/**
30+
* Creates a clone of an existing template
31+
*
32+
* @param id Identifier of the original template
33+
* @param cloneName Name of the cloned template
34+
*
35+
* @return the created clone
36+
*
37+
* @throws PersistenceException Error in the data layer
2338
*/
24-
public Template findByName(String name, long tenantId);
39+
public Template clone(long id, String cloneName) throws PersistenceException;
2540

2641
/**
2742
* Counts the total number of documents using this template
2843
*
2944
* @param id The template ID
3045
* @return the documents count
46+
*
47+
* @throws PersistenceException Error in the data layer
3148
*/
32-
public int countDocs(long id);
49+
public int countDocs(long id) throws PersistenceException;
3350

3451
/**
3552
* This method finds a template by type
@@ -38,8 +55,10 @@ public interface TemplateDAO extends PersistentObjectDAO<Template> {
3855
* @param tenantId ID of the owning tenant
3956
*
4057
* @return Template with given type
58+
*
59+
* @throws PersistenceException Error in the data layer
4160
*/
42-
public List<Template> findByType(int type, long tenantId);
61+
public List<Template> findByType(int type, long tenantId) throws PersistenceException;
4362

4463
/**
4564
* Finds all permissions of a user enabled on the specified template
@@ -71,6 +90,5 @@ public interface TemplateDAO extends PersistentObjectDAO<Template> {
7190
*/
7291
public boolean isReadEnable(long templateId, long userId);
7392

74-
7593
public void initialize(Template template);
7694
}

logicaldoc-core/src/main/java/com/logicaldoc/core/metadata/TemplateGroup.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public TemplateGroup() {
2525
public TemplateGroup(long groupId) {
2626
this.groupId = groupId;
2727
}
28+
29+
public TemplateGroup(long groupId, int write) {
30+
this.groupId = groupId;
31+
this.write = write;
32+
}
2833

2934
public TemplateGroup(TemplateGroup source) {
3035
this.write = source.write;

logicaldoc-core/src/main/java/com/logicaldoc/core/parser/AbstractParser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ private void parseInCurrentThread(final InputStream input, ParseParameters param
112112
try {
113113
internalParse(input, parameters, content);
114114
} catch (ParseException pe) {
115+
log.error(pe.getMessage(), pe);
115116
throw pe;
116117
} catch (Exception e) {
118+
log.error(e.getMessage(), e);
117119
throw new ParseException(e);
118120
}
119121
}

logicaldoc-core/src/test/java/com/logicaldoc/core/metadata/HibernateTemplateDAOTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void testFindAll() {
5252
Assert.assertNotNull(templates);
5353
Assert.assertEquals(4, templates.size());
5454
}
55-
55+
5656
@Test
5757
public void testFindById() throws PersistenceException {
5858
Template template = dao.findById(1);
@@ -75,7 +75,25 @@ public void testFindById() throws PersistenceException {
7575
}
7676

7777
@Test
78-
public void testFindByName() {
78+
public void testClone() throws PersistenceException {
79+
Template template = dao.findById(1);
80+
Assert.assertNotNull(template);
81+
dao.initialize(template);
82+
Assert.assertEquals(1, template.getId());
83+
Assert.assertEquals("test1", template.getName());
84+
Assert.assertTrue(template.getAttributes().containsKey("attr1"));
85+
86+
Template clone = dao.clone(1, "test1-Clone");
87+
Assert.assertNotNull(clone);
88+
clone = dao.findById(clone.getId());
89+
dao.initialize(clone);
90+
Assert.assertNotSame(1, clone.getId());
91+
Assert.assertEquals("test1-Clone", clone.getName());
92+
Assert.assertTrue(clone.getAttributes().containsKey("attr1"));
93+
}
94+
95+
@Test
96+
public void testFindByName() throws PersistenceException {
7997
Template template = dao.findByName("test1", Tenant.DEFAULT_ID);
8098
Assert.assertNotNull(template);
8199
dao.initialize(template);

logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/metadata/template/TemplatesPanel.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void onDraw() {
9292
list.setAutoFetchData(true);
9393
list.setWidth100();
9494
list.setHeight100();
95-
list.setFields(name, label, description);
95+
list.setFields(id, name, label, description);
9696
list.setSelectionType(SelectionStyle.SINGLE);
9797
list.setShowRecordComponents(true);
9898
list.setShowRecordComponentsByCell(true);
@@ -164,14 +164,14 @@ public void onSuccess(GUITemplate template) {
164164
private void showContextMenu() {
165165
Menu contextMenu = new Menu();
166166

167-
final ListGridRecord rec = list.getSelectedRecord();
168-
final long id = Long.parseLong(rec.getAttributeAsString("id"));
167+
final ListGridRecord selectedRecord = list.getSelectedRecord();
168+
final long selectedTemplateId = selectedRecord.getAttributeAsLong("id");
169169

170170
MenuItem delete = new MenuItem();
171171
delete.setTitle(I18N.message("ddelete"));
172172
delete.addClickHandler(event -> LD.ask(I18N.message("question"), I18N.message("confirmdelete"), answer -> {
173173
if (Boolean.TRUE.equals(answer)) {
174-
TemplateService.Instance.get().delete(id, new AsyncCallback<Void>() {
174+
TemplateService.Instance.get().delete(selectedTemplateId, new AsyncCallback<Void>() {
175175
@Override
176176
public void onFailure(Throwable caught) {
177177
GuiLog.serverError(caught);
@@ -187,7 +187,33 @@ public void onSuccess(Void result) {
187187
}
188188
}));
189189

190-
contextMenu.setItems(delete);
190+
MenuItem clone = new MenuItem();
191+
clone.setTitle(I18N.message("clone"));
192+
clone.addClickHandler(event -> TemplateService.Instance.get().clone(selectedTemplateId,
193+
selectedRecord.getAttribute("name") + "-Clone", new AsyncCallback<GUITemplate>() {
194+
@Override
195+
public void onFailure(Throwable caught) {
196+
GuiLog.serverError(caught);
197+
}
198+
199+
@Override
200+
public void onSuccess(GUITemplate templateClone) {
201+
list.deselectAllRecords();
202+
ListGridRecord newRecord = new ListGridRecord();
203+
newRecord.setAttribute("id", templateClone.getId());
204+
newRecord.setAttribute("readonly", "" + templateClone.isReadonly());
205+
newRecord.setAttribute("name", templateClone.getName());
206+
newRecord.setAttribute(LABEL,
207+
templateClone.getLabel() != null ? templateClone.getLabel() : templateClone.getName());
208+
newRecord.setAttribute(DESCRIPTION, templateClone.getDescription());
209+
list.addData(newRecord);
210+
list.selectRecord(newRecord);
211+
list.scrollToRow(list.getRowNum(newRecord));
212+
showTemplateDetails(templateClone);
213+
}
214+
}));
215+
216+
contextMenu.setItems(clone, delete);
191217
contextMenu.showContextMenu();
192218
}
193219

@@ -221,7 +247,7 @@ public void updateRecord(GUITemplate template) {
221247

222248
rec.setAttribute("readonly", "" + template.isReadonly());
223249
rec.setAttribute("name", template.getName());
224-
rec.setAttribute(LABEL, template.getLabel()!=null ? template.getLabel(): template.getName());
250+
rec.setAttribute(LABEL, template.getLabel() != null ? template.getLabel() : template.getName());
225251
rec.setAttribute(DESCRIPTION, template.getDescription());
226252
list.refreshRow(list.getRecordIndex(rec));
227253
}

0 commit comments

Comments
 (0)