-
Notifications
You must be signed in to change notification settings - Fork 461
/
FieldAPIImpl.java
960 lines (820 loc) · 40.1 KB
/
FieldAPIImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
package com.dotcms.contenttype.business;
import com.dotcms.api.system.event.message.MessageSeverity;
import com.dotcms.api.system.event.message.MessageType;
import com.dotcms.api.system.event.message.SystemMessageEventUtil;
import com.dotcms.api.system.event.message.builder.SystemMessageBuilder;
import com.dotcms.business.CloseDBIfOpened;
import com.dotcms.business.WrapInTransaction;
import com.dotcms.content.elasticsearch.business.IndiciesInfo;
import com.dotcms.content.elasticsearch.util.ESMappingUtilHelper;
import com.dotcms.contenttype.exception.NotFoundInDbException;
import com.dotcms.contenttype.model.field.BinaryField;
import com.dotcms.contenttype.model.field.CategoryField;
import com.dotcms.contenttype.model.field.CheckboxField;
import com.dotcms.contenttype.model.field.ColumnField;
import com.dotcms.contenttype.model.field.ConstantField;
import com.dotcms.contenttype.model.field.CustomField;
import com.dotcms.contenttype.model.field.DateField;
import com.dotcms.contenttype.model.field.DateTimeField;
import com.dotcms.contenttype.model.field.Field;
import com.dotcms.contenttype.model.field.FieldBuilder;
import com.dotcms.contenttype.model.field.FieldVariable;
import com.dotcms.contenttype.model.field.FileField;
import com.dotcms.contenttype.model.field.HiddenField;
import com.dotcms.contenttype.model.field.HostFolderField;
import com.dotcms.contenttype.model.field.ImageField;
import com.dotcms.contenttype.model.field.ImmutableFieldVariable;
import com.dotcms.contenttype.model.field.JSONField;
import com.dotcms.contenttype.model.field.KeyValueField;
import com.dotcms.contenttype.model.field.LineDividerField;
import com.dotcms.contenttype.model.field.MultiSelectField;
import com.dotcms.contenttype.model.field.PermissionTabField;
import com.dotcms.contenttype.model.field.RadioField;
import com.dotcms.contenttype.model.field.RelationshipField;
import com.dotcms.contenttype.model.field.RelationshipsTabField;
import com.dotcms.contenttype.model.field.RowField;
import com.dotcms.contenttype.model.field.SelectField;
import com.dotcms.contenttype.model.field.StoryBlockField;
import com.dotcms.contenttype.model.field.TabDividerField;
import com.dotcms.contenttype.model.field.TagField;
import com.dotcms.contenttype.model.field.TextAreaField;
import com.dotcms.contenttype.model.field.TimeField;
import com.dotcms.contenttype.model.field.WysiwygField;
import com.dotcms.contenttype.model.field.event.FieldDeletedEvent;
import com.dotcms.contenttype.model.field.event.FieldSavedEvent;
import com.dotcms.contenttype.model.type.ContentType;
import com.dotcms.contenttype.transform.contenttype.ContentTypeInternationalization;
import com.dotcms.contenttype.transform.contenttype.StructureTransformer;
import com.dotcms.exception.ExceptionUtil;
import com.dotcms.languagevariable.business.LanguageVariableAPI;
import com.dotcms.rendering.velocity.services.ContentTypeLoader;
import com.dotcms.rendering.velocity.services.ContentletLoader;
import com.dotcms.repackage.com.google.common.annotations.VisibleForTesting;
import com.dotcms.system.event.local.business.LocalSystemEventsAPI;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.business.FactoryLocator;
import com.dotmarketing.business.PermissionAPI;
import com.dotmarketing.business.PermissionLevel;
import com.dotmarketing.business.RelationshipAPI;
import com.dotmarketing.business.UserAPI;
import com.dotmarketing.db.HibernateUtil;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotDataValidationException;
import com.dotmarketing.exception.DotRuntimeException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.structure.model.Relationship;
import com.dotmarketing.portlets.structure.model.Structure;
import com.dotmarketing.quartz.job.CleanUpFieldReferencesJob;
import com.dotmarketing.util.ActivityLogger;
import com.dotmarketing.util.Config;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;
import com.dotmarketing.util.WebKeys.Relationship.RELATIONSHIP_CARDINALITY;
import com.dotmarketing.util.json.JSONException;
import com.dotmarketing.util.json.JSONObject;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.liferay.portal.language.LanguageException;
import com.liferay.portal.language.LanguageUtil;
import com.liferay.portal.model.User;
import io.vavr.control.Try;
import org.apache.commons.lang.StringUtils;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static com.dotcms.contenttype.model.type.PageContentType.PAGE_FRIENDLY_NAME_FIELD_VAR;
import static com.dotcms.util.CollectionsUtils.list;
public class FieldAPIImpl implements FieldAPI {
private final List<Class<? extends Field>> baseFieldTypes = ImmutableList.of(BinaryField.class, StoryBlockField.class,
CategoryField.class, ConstantField.class, CheckboxField.class, CustomField.class, DateField.class,
DateTimeField.class, FileField.class, HiddenField.class, HostFolderField.class,
ImageField.class, KeyValueField.class, LineDividerField.class, MultiSelectField.class,
PermissionTabField.class, RadioField.class, RelationshipField.class, RelationshipsTabField.class, SelectField.class,
TabDividerField.class, TagField.class, TextAreaField.class, TimeField.class,
WysiwygField.class);
private final PermissionAPI permissionAPI;
private final ContentletAPI contentletAPI;
private final UserAPI userAPI;
private final RelationshipAPI relationshipAPI;
private final LocalSystemEventsAPI localSystemEventsAPI;
private final LanguageVariableAPI languageVariableAPI;
private final FieldFactory fieldFactory;
public FieldAPIImpl() {
this(APILocator.getPermissionAPI(),
APILocator.getContentletAPI(),
APILocator.getUserAPI(),
APILocator.getRelationshipAPI(),
APILocator.getLocalSystemEventsAPI(),
APILocator.getLanguageVariableAPI(),
FactoryLocator.getFieldFactory());
}
@VisibleForTesting
public FieldAPIImpl(final PermissionAPI perAPI,
final ContentletAPI conAPI,
final UserAPI userAPI,
final RelationshipAPI relationshipAPI,
final LocalSystemEventsAPI localSystemEventsAPI,
final LanguageVariableAPI languageVariableAPI,
final FieldFactory fieldFactory) {
this.permissionAPI = perAPI;
this.contentletAPI = conAPI;
this.userAPI = userAPI;
this.relationshipAPI = relationshipAPI;
this.localSystemEventsAPI = localSystemEventsAPI;
this.languageVariableAPI = languageVariableAPI;
this.fieldFactory = fieldFactory;
}
@WrapInTransaction
@Override
public Field save(final Field field, final User user) throws DotDataException, DotSecurityException {
return save(field, user, true);
}
@WrapInTransaction
@Override
public Field save(final Field field, final User user, final boolean reorder)
throws DotDataException, DotSecurityException {
if(!UtilMethods.isSet(field.contentTypeId())){
Logger.error(this, "ContentTypeId needs to be set to save the Field");
throw new DotDataValidationException("ContentTypeId needs to be set to save the Field");
}
ContentTypeAPI contentTypeAPI = APILocator.getContentTypeAPI(user);
ContentType type = contentTypeAPI.find(field.contentTypeId()) ;
permissionAPI.checkPermission(type, PermissionLevel.EDIT_PERMISSIONS, user);
Field oldField = null;
boolean useFriendlyNameOldVarName = false;
if (UtilMethods.isSet(field.id())) {
try {
oldField = fieldFactory.byId(field.id());
final Field newFieldCopy = FieldBuilder.builder(field).modDate(oldField.modDate()).build();
if(newFieldCopy.equals(oldField)) {
Logger.info(this, "No changes made to field: " + field.variable() + ", returning");
return field;
}
if(!oldField.variable().equals(field.variable())){
//TODO: Remove this condition for future releases.
// It has been done to keep backward compatibility -> Issue: https://github.com/dotCMS/core/issues/17239
if (oldField.variable().equalsIgnoreCase(field.variable()) && oldField
.variable().equals(PAGE_FRIENDLY_NAME_FIELD_VAR)) {
useFriendlyNameOldVarName = true;
} else {
throw new DotDataValidationException(
"Field variable can not be modified, please use the following: "
+ oldField.variable());
}
}
if(!oldField.contentTypeId().equals(field.contentTypeId()) ){
throw new DotDataValidationException("Field content type can not be modified, "
+ "please use the following: " + oldField.contentTypeId());
}
if (reorder && oldField.sortOrder() != field.sortOrder()){
if (oldField.sortOrder() > field.sortOrder()) {
fieldFactory.moveSortOrderForward(type.id(), field.sortOrder(), oldField.sortOrder());
} else {
fieldFactory.moveSortOrderBackward(type.id(), oldField.sortOrder(), field.sortOrder());
}
}
if (oldField instanceof RelationshipField && null != oldField.relationType()
&& null != field.relationType() && !oldField.relationType()
.equals(field.relationType())) {
Logger.error(this,
"Related content type cannot be modified. A new relationship field should be created instead");
throw new DotDataException(
"Related content type cannot be modified. A new relationship field should be created instead");
}
} catch(NotFoundInDbException e) {
//Do nothing as Starter comes with id but field is unexisting yet
}
}else {
//This validation should only be for new fields, since the field velocity var name(variable) can not be modified
if(UtilMethods.isSet(field.variable()) && !field.variable().matches("^[A-Za-z][0-9A-Za-z]*")) {
final String errorMessage = "Field velocity var name "+ field.variable() +" contains characters not allowed, here is a suggestion of the variable: " + com.dotmarketing.util.StringUtils.camelCaseLower(field.variable());
Logger.error(this, errorMessage);
throw new DotDataValidationException(errorMessage);
}
if (reorder) {
fieldFactory.moveSortOrderForward(type.id(), field.sortOrder());
}
}
if (field instanceof RelationshipField && !(((RelationshipField)field).skipRelationshipCreation())) {
validateRelationshipField(field);
}
//TODO: Remove this condition for future releases.
// It has been done to keep backward compatibility -> Issue: https://github.com/dotCMS/core/issues/17239
Field result = fieldFactory
.save(useFriendlyNameOldVarName ? FieldBuilder.builder(field).variable(oldField.variable())
.build() : field);
//if RelationshipField, Relationship record must be added/updated
if (field instanceof RelationshipField && !(((RelationshipField)field).skipRelationshipCreation())) {
Optional<Relationship> relationship = getRelationshipForField(result, contentTypeAPI,
type, user);
if (relationship.isPresent()) {
relationshipAPI.save(relationship.get());
Logger.info(this,
"The relationship has been saved successfully for field " + field.name());
}
//update field reference (in case it might have been modified)
result = fieldFactory.byId(result.id());
}
//update Content Type mod_date to detect the changes done on the field
contentTypeAPI.updateModDate(type);
Structure structure = new StructureTransformer(type).asStructure();
CacheLocator.getContentTypeCache().remove(structure);
new ContentTypeLoader().invalidate(structure);
if(oldField!=null){
if(oldField.indexed() != field.indexed()){
contentletAPI.refresh(structure);
} else if (field instanceof ConstantField) {
if(!StringUtils.equals(oldField.values(), field.values()) ){
new ContentletLoader().invalidate(structure);
contentletAPI.refresh(structure);
}
}
ActivityLogger.logInfo(ActivityLogger.class, "Update Field Action",
String.format("User %s/%s modified field %s to %s Structure.", user.getUserId(), user.getFirstName(),
field.name(), structure.getName()));
} else {
//If saving a new indexed field, it should try to set an ES mapping for the field
if (result.indexed()) {
addESMappingForField(structure, result);
}
ActivityLogger.logInfo(ActivityLogger.class, "Save Field Action",
String.format("User %s/%s added field %s to %s Structure.", user.getUserId(), user.getFirstName(), field.name(),
structure.getName()));
}
Field finalResult = result;
HibernateUtil.addCommitListener(()-> {
localSystemEventsAPI.notify(new FieldSavedEvent(finalResult));
});
return result;
}
/**
* This method tries to set an ES mapping for the field.
* In case of failure, we just log a warning and continue with the transaction
* @param field
*/
private void addESMappingForField(final Structure structure, final Field field) {
try {
final IndiciesInfo indiciesInfo = APILocator.getIndiciesAPI().loadIndicies();
if (indiciesInfo != null){
if (UtilMethods.isSet(indiciesInfo.getLive())) {
ESMappingUtilHelper.getInstance().addCustomMapping(field, indiciesInfo.getLive());
Logger.info(this.getClass(), String.format(
"Elasticsearch mapping set for Field: %s. Content type: %s on Index: %s",
field.name(), structure.getName(), APILocator.getESIndexAPI()
.removeClusterIdFromName(indiciesInfo.getLive())));
}
if (UtilMethods.isSet(indiciesInfo.getWorking())) {
ESMappingUtilHelper.getInstance().addCustomMapping(field, indiciesInfo.getWorking());
Logger.info(this.getClass(), String.format(
"Elasticsearch mapping set for Field: %s. Content type: %s on Index: %s",
field.name(), structure.getName(), APILocator.getESIndexAPI()
.removeClusterIdFromName(indiciesInfo.getWorking())));
}
}
} catch (Exception e) {
Logger.warnAndDebug(this.getClass(), String.format(
"Error trying to set Elasticsearch mapping for Field: %s. Content type: %s",
field.name(), structure.getName()), e);
}
}
/**
* Validates that properties n a relationship field are set correctly
* @param field
* @throws DotDataException
*/
private void validateRelationshipField(Field field) throws DotDataValidationException {
String errorMessage = null;
if(!UtilMethods.isSet(field.relationType())){
errorMessage = "Relation Type needs to be set";
}
if (!field.indexed()){
errorMessage = "Relationship Field " + field.name() + " must always be indexed";
}
if (field.listed()){
errorMessage = "Relationship Field " + field.name() + " cannot be listed";
}
if(!UtilMethods.isSet(field.values())){
errorMessage = "Relationship Cardinality needs to be set";
}else {
final int cardinality = Integer.parseInt(field.values());
//check if cardinality is valid
if (cardinality < 0 || cardinality >= RELATIONSHIP_CARDINALITY.values().length) {
errorMessage = "Cardinality value is incorrect";
}
}
if (errorMessage != null){
Logger.error(this, errorMessage);
throw new DotDataValidationException(errorMessage);
}
}
/**
* Verify if a relationship already exists for this field. Otherwise, a new relationship object
* is created
* @param field
* @param contentTypeAPI
* @param type
* @return
* @throws DotDataException
*/
@VisibleForTesting
Optional<Relationship> getRelationshipForField(final Field field, final ContentTypeAPI contentTypeAPI,
final ContentType type, final User user) throws DotDataException, DotSecurityException {
Relationship relationship;
ContentType relatedContentType;
try {
final int cardinality = Integer.parseInt(field.values());
final String[] relationType = field.relationType().split("\\.");
//we need to find the id of the related structure using the velocityVarName set in the relationType
try {
relatedContentType = contentTypeAPI.find(relationType[0]);
} catch (NotFoundInDbException e) {
final String errorMessage = "Unable to save relationships for field " + field.name()
+ " because the related content type " + relationType[0]
+ " was not found in DB."
+ " A new attempt will be made when the related content type is saved.";
Logger.info(this, errorMessage);
Logger.debug(this, errorMessage, e);
return Optional.empty();
}
//getting the existing relationship
relationship = relationshipAPI.getRelationshipFromField(field, user);
//verify if the relationship already exists
if (UtilMethods.isSet(relationship) && UtilMethods.isSet(relationship.getInode())) {
updateRelationshipObject(field, type, relatedContentType, relationship, cardinality, user);
} else {
//otherwise, a new relationship will be created
relationship = new Relationship(type, relatedContentType, field);
}
} catch (DotSecurityException e) {
Logger.error(this, "Error saving relationship for field: " + field.variable(), e);
throw e;
} catch (Exception e) {
//we need to capture any error found during relationship creation
//(ie.: NumberFormatException, NullPointerException, ArrayOutOfBoundException)
Logger.error(this, "Error saving relationship for field: " + field.variable(), e);
throw new DotDataException(e);
}
return Optional.of(relationship);
}
/**
* Update properties in a existing relationship
* In case the `required` property is set to true, the other side of the relationship (if exists) will be switched to not required
* @param field
* @param type
* @param relatedContentType
* @param relationship
* @param cardinality
* @throws DotDataException
*/
private void updateRelationshipObject(final Field field, final ContentType type, final ContentType relatedContentType,
final Relationship relationship, final int cardinality, final User user)
throws DotDataException {
final boolean isChildField;
final String relationName = field.variable();
FieldBuilder builder;
if (relationshipAPI.sameParentAndChild(relationship)){
isChildField = relationship.getParentRelationName() != null && relationship
.getParentRelationName().equals(field.variable()) || (
relationship.getParentRelationName() == null && !relationship
.getChildRelationName().equals(field.variable()));
} else{
isChildField = relationship.getChildStructureInode().equals(type.id());
}
//check which side of the relationship is being updated (parent or child)
if (isChildField) {
//parent is updated
relationship.setParentRelationName(relationName);
relationship.setParentRequired(field.required());
//only one side of the relationship can be required
if (relationship.getChildRelationName() != null && field.required() && relationship.isChildRequired()) {
//setting as not required this side of the relationship
relationship.setParentRequired(false);
sendRelationshipErrorMessage(relationship.getParentRelationName(), user);
builder = FieldBuilder.builder(field);
builder.required(false);
fieldFactory.save(builder.build());
}
if (relationship.getChildRelationName() != null) {
//verify if the cardinality was changed to update it on the other side of the relationship
final Field otherSideField = byContentTypeAndVar(relatedContentType,
relationship.getChildRelationName());
if (!otherSideField.values().equals(field.values())) {
//if cardinality changes, the other side field will be updated with the new cardinality
builder = FieldBuilder.builder(otherSideField);
fieldFactory.save(builder.values(field.values()).build());
}
}
} else {
//child is updated
relationship.setChildRelationName(relationName);
relationship.setChildRequired(field.required());
//only one side of the relationship can be required
if (field.required() && relationship.getParentRelationName() != null && relationship.isParentRequired()) {
//setting this side of the relationship as not required
relationship.setChildRequired(false);
sendRelationshipErrorMessage(relationship.getChildRelationName(), user);
builder = FieldBuilder.builder(field);
builder.required(false);
fieldFactory.save(builder.values(field.values()).build());
}
//verify if the cardinality was changed to update it on the other side of the relationship
if (relationship.getParentRelationName() != null) {
final Field otherSideField = Try.of(()->byContentTypeAndVar(relatedContentType,
relationship.getParentRelationName())).getOrNull();
if (otherSideField!=null && !otherSideField.values().equals(field.values())) {
//if cardinality changes, the other side field will be updated with the new cardinality
builder = FieldBuilder.builder(otherSideField);
fieldFactory.save(builder.values(field.values()).build());
}
}
}
relationship.setCardinality(cardinality);
}
private void sendRelationshipErrorMessage(
final String relationName,
final User user
) {
final SystemMessageEventUtil systemMessageEventUtil = SystemMessageEventUtil.getInstance();
try {
systemMessageEventUtil.pushMessage(
new SystemMessageBuilder()
.setMessage(LanguageUtil.format(
user.getLocale(),
"contenttypes.field.properties.relationship.required.error",
relationName)
)
.setSeverity(MessageSeverity.INFO)
.setType(MessageType.SIMPLE_MESSAGE)
.setLife(6000)
.create(),
list(user.getUserId())
);
} catch (LanguageException e) {
throw new DotRuntimeException(e);
}
}
@WrapInTransaction
@Override
public FieldVariable save(final FieldVariable var, final User user) throws DotDataException, DotSecurityException {
ContentTypeAPI contentTypeAPI = APILocator.getContentTypeAPI(user);
Field field = fieldFactory.byId(var.fieldId());
ContentType type = contentTypeAPI.find(field.contentTypeId()) ;
APILocator.getPermissionAPI().checkPermission(type, PermissionLevel.EDIT_PERMISSIONS, user);
FieldVariable newFieldVariable = fieldFactory.save(ImmutableFieldVariable.builder().from(var).userId(user.getUserId()).build());
//update Content Type mod_date to detect the changes done on the field variables
contentTypeAPI.updateModDate(type);
//Validates custom mapping format
if (var.key().equals(FieldVariable.ES_CUSTOM_MAPPING_KEY)){
try {
new JSONObject(var.value());
} catch (JSONException e) {
handleInvalidCustomMappingError(var, user, field, type, e);
}
//Verifies the field is marked as System Indexed. In case it isn't, the field will be updated with this flag on
if (!field.indexed()) {
save(FieldBuilder.builder(field).indexed(true).build(), user);
Logger.info(this, "Field " + type.variable() + "." + field.variable()
+ " has been marked as System Indexed as it has defined a field variable with key "
+ FieldVariable.ES_CUSTOM_MAPPING_KEY);
}
}
return newFieldVariable;
}
/**
* Sends a system message (warning) when a custom mapping is invalid
* @param fieldVariable
* @param user
* @param field
* @param type
* @param exception
*/
private void handleInvalidCustomMappingError(final FieldVariable fieldVariable, final User user,
final Field field, final ContentType type, final JSONException exception) {
final String message;
try {
message = "Invalid format on field variable value. Value should be a JSON object. Field variable: "
+ fieldVariable.key() + ". Field: " + field.name() + ". Content Type: " + type
.name();
final SystemMessageEventUtil systemMessageEventUtil = SystemMessageEventUtil.getInstance();
systemMessageEventUtil.pushMessage(
new SystemMessageBuilder()
.setMessage(LanguageUtil.get(
user.getLocale(),
"message.fieldvariables.invalid.custom.mapping"))
.setSeverity(MessageSeverity.WARNING)
.setType(MessageType.SIMPLE_MESSAGE)
.setLife(6000)
.create(),
list(user.getUserId())
);
Logger.warnAndDebug(FieldAPIImpl.class, message, exception);
} catch (LanguageException ex) {
throw new DotRuntimeException(ex);
}
}
@Override
public void delete(final Field field) throws DotDataException {
try {
this.delete(field, this.userAPI.getSystemUser());
} catch (DotSecurityException e){
throw new DotDataException(e);
}
}
@WrapInTransaction
@Override
public void delete(final Field field, final User user) throws DotDataException, DotSecurityException {
final ContentTypeAPI contentTypeAPI = APILocator.getContentTypeAPI(user);
final ContentType type = contentTypeAPI.find(field.contentTypeId());
permissionAPI.checkPermission(type, PermissionLevel.EDIT_PERMISSIONS, user);
Field oldField = fieldFactory.byId(field.id());
if(oldField.fixed() || oldField.readOnly()){
throw new DotDataException("You cannot delete a fixed or read only field");
}
final Structure structure = new StructureTransformer(type).asStructure();
fieldFactory.moveSortOrderBackward(type.id(), oldField.sortOrder());
fieldFactory.delete(field);
ActivityLogger.logInfo(ActivityLogger.class, "Delete Field Action",
String.format("User %s/%s deleted field %s from %s Content Type.", user.getUserId(), user.getFirstName(),
field.name(), structure.getName()));
//update Content Type mod_date to detect the changes done on the field
contentTypeAPI.updateModDate(type);
CacheLocator.getContentTypeCache().remove(structure);
//if RelationshipField, Relationship record must be updated/deleted
if (field instanceof RelationshipField) {
removeRelationshipLink(field, type, contentTypeAPI);
}
// rebuild contentlets indexes
if(field.indexed()){
contentletAPI.reindex(structure);
}
CleanUpFieldReferencesJob.triggerCleanUpJob(field, user);
localSystemEventsAPI.notify(new FieldDeletedEvent(field.variable()));
}
/**
* Given a field load and return its variables.
*
* @param field field variables belong to
* @return list of variables
* @throws DotDataException when SQL error happens
*/
@Override
@CloseDBIfOpened
public List<FieldVariable> loadVariables(final Field field) throws DotDataException {
return UtilMethods.isSet(field) ? fieldFactory.loadVariables(field) : Collections.emptyList();
}
/**
* Remove one-sided relationship when the field is deleted
* @param field
* @param type
* @param contentTypeAPI
* @throws DotDataException
*/
private void removeRelationshipLink(final Field field, final ContentType type,
final ContentTypeAPI contentTypeAPI)
throws DotDataException, DotSecurityException {
final Optional<Relationship> result = relationshipAPI
.byParentChildRelationName(type, field.variable());
if (result.isPresent()) {
Relationship relationship = result.get();
//it's a one-sided relationship and must be deleted
if (!UtilMethods.isSet(relationship.getParentRelationName()) || !UtilMethods
.isSet(relationship.getChildRelationName())) {
relationshipAPI.delete(relationship);
} else {
//the relationship must be updated, removing one side of the relationship
if (UtilMethods.isSet(relationship.getParentRelationName()) && relationship
.getParentRelationName().equals(field.variable())) {
unlinkParent(relationship);
} else {
unlinkChild(relationship);
}
relationshipAPI.save(relationship);
}
//If it is not a self-relationship, the other content type must be reindexed
//The current content type is reindexed in the delete method
if (!relationshipAPI.sameParentAndChild(relationship)) {
Structure otherSideStructure;
if (relationship.getChildStructureInode().equals(field.contentTypeId())) {
otherSideStructure = new StructureTransformer(
contentTypeAPI.find(relationship.getParentStructureInode()))
.asStructure();
} else {
otherSideStructure = new StructureTransformer(
contentTypeAPI.find(relationship.getChildStructureInode()))
.asStructure();
}
contentletAPI.reindex(otherSideStructure);
}
}
}
/**
*
* @param relationship
*/
private void unlinkParent(final Relationship relationship) {
relationship.setParentRequired(false);
relationship.setParentRelationName(null);
}
/**
*
* @param relationship
*/
private void unlinkChild(final Relationship relationship) {
relationship.setChildRequired(false);
relationship.setChildRelationName(null);
}
@CloseDBIfOpened
@Override
public List<Field> byContentTypeId(final String typeId) throws DotDataException {
return fieldFactory.byContentTypeId(typeId);
}
@CloseDBIfOpened
@Override
public String nextAvailableColumn(final Field field) throws DotDataException{
return fieldFactory.nextAvailableColumn(field);
}
@CloseDBIfOpened
@Override
public Field find(final String id) throws DotDataException {
return fieldFactory.byId(id);
}
@CloseDBIfOpened
@Override
public Field byContentTypeAndVar(final ContentType type, final String fieldVar) throws DotDataException {
return fieldFactory.byContentTypeFieldVar(type, fieldVar);
}
@CloseDBIfOpened
@Override
public Optional<Field> byContentTypeAndFieldRelationType(final String id,
final String fieldRelationType) throws DotDataException {
return fieldFactory.byContentTypeIdFieldRelationTypeInDb(id, fieldRelationType);
}
@CloseDBIfOpened
@Override
public Field byContentTypeIdAndVar(final String id, final String fieldVar) throws DotDataException {
try {
return byContentTypeAndVar(APILocator.getContentTypeAPI(APILocator.systemUser()).find(id), fieldVar);
} catch (DotSecurityException e) {
throw new DotDataException(e);
}
}
@WrapInTransaction
@Override
public void deleteFieldsByContentType(final ContentType type) throws DotDataException {
final List<Field> fields = byContentTypeId(type.id());
for (Field field : fields) {
delete(field);
}
}
@Override
public List<Class<? extends Field>> fieldTypes() {
return baseFieldTypes;
}
@Override
public void registerFieldType(Field type) {
throw new DotStateException("Not implemented");
}
@Override
public void deRegisterFieldType(Field type) {
throw new DotStateException("Not implemented");
}
@WrapInTransaction
@Override
public void delete(final FieldVariable fieldVar) throws DotDataException {
fieldFactory.delete(fieldVar);
Field field = this.find(fieldVar.fieldId());
ContentTypeAPI contentTypeAPI = APILocator.getContentTypeAPI(this.userAPI.getSystemUser());
ContentType type;
try {
type = contentTypeAPI.find(field.contentTypeId());
//update Content Type mod_date to detect the changes done on the field variable
contentTypeAPI.updateModDate(type);
} catch (DotSecurityException e) {
throw new DotDataException("Error updating Content Type mode_date for FieldVariable("+fieldVar.id()+"). "+e.getMessage());
}
}
/**
* Delete a bunch of fields, if a Exception is throw deleting any field then no field is delete
*
* @param fieldsID fields's id to delete
* @param user user who delete the fields
* @return
* @throws DotDataException
* @throws DotSecurityException
*/
@WrapInTransaction
public Collection<String> deleteFields(final List<String> fieldsID, final User user) throws DotDataException, DotSecurityException {
final List<String> deleteIds = new ArrayList<>();
for (final String fieldId : fieldsID) {
try {
final Field field = find(fieldId);
delete(field, user);
deleteIds.add(field.id());
} catch (NotFoundInDbException e) {
continue;
}
}
return deleteIds;
}
/**
* Save a bunch of fields, , if a Exception is throw deleting any field then no field is save
*
* @param fields fields to save
* @param user user who save the fields
* @throws DotSecurityException
* @throws DotDataException
*/
@WrapInTransaction
public void saveFields(final List<Field> fields, final User user) throws DotSecurityException, DotDataException {
for (final Field field : fields) {
save(field, user, false);
}
}
@Override
public Map<String, Object> getFieldInternationalization(
final ContentType contentType,
final ContentTypeInternationalization contentTypeInternationalization,
final Map<String, Object> fieldMap
) {
return getFieldInternationalization(contentType, contentTypeInternationalization, fieldMap, APILocator.systemUser());
}
@Override
public Map<String, Object> getFieldInternationalization(
final ContentType contentType,
final ContentTypeInternationalization contentTypeInternationalization,
final Map<String, Object> fieldMap,
final User user
) {
final long languageId = contentTypeInternationalization.getLanguageId();
final boolean live = contentTypeInternationalization.isLive();
try {
final ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
for (final String propertyName : fieldMap.keySet()) {
final String key = String.format("%s.%s.%s", contentType.variable(), fieldMap.get("variable"), propertyName);
final String i18nValue = this.languageVariableAPI.getLanguageVariable(
key, languageId, user, live, user == null);
if (!i18nValue.equals(key) && !i18nValue.equals(fieldMap.get(propertyName).toString())) {
builder.put(propertyName, i18nValue);
} else {
builder.put(propertyName, fieldMap.get(propertyName));
}
}
return builder.build();
} catch (DotRuntimeException e) {
if (ExceptionUtil.causedBy(e, ConnectException.class)) {
return new ImmutableMap.Builder<String, Object>().putAll(fieldMap).build();
} else {
throw e;
}
}
}
@Override
public boolean isFullScreenField(final com.dotcms.contenttype.model.field.Field field) {
if(!Config.getBooleanProperty(FieldAPI.FULLSCREEN_FIELD_FEATURE_FLAG, true)){
return false;
}
try {
final ContentType type = APILocator.getContentTypeAPI(APILocator.systemUser()).find(field.contentTypeId());
if (!(field instanceof WysiwygField ||
field instanceof StoryBlockField ||
field instanceof TextAreaField ||
field instanceof CustomField ||
field instanceof JSONField
)) {
return false;
}
boolean showFullScreen = Try.of(() -> Boolean.parseBoolean(field.fieldVariablesMap().get("showFullScreen").value())).getOrElse(true);
if (!showFullScreen) {
return false;
}
final List<com.dotcms.contenttype.model.field.Field> fields = type.fields();
com.dotcms.contenttype.model.field.Field pppField = Try.of(() -> fields.get(fields.indexOf(field) - 3)).getOrNull();
com.dotcms.contenttype.model.field.Field ppField = Try.of(() -> fields.get(fields.indexOf(field) - 2)).getOrNull();
com.dotcms.contenttype.model.field.Field pField = Try.of(() -> fields.get(fields.indexOf(field) - 1)).getOrNull();
com.dotcms.contenttype.model.field.Field nextField = Try.of(() -> fields.get(fields.indexOf(field) + 1)).getOrNull();
// we are either in a tab or the first field
if (pppField != null && !(pppField instanceof TabDividerField)) {
return false;
}
// we are the last field in the type or there is a column next
if (nextField != null && !(nextField instanceof TabDividerField)) {
return false;
}
// there is a row > column before us
if (!(ppField instanceof RowField) || !(pField instanceof ColumnField)) {
return false;
}
return true;
} catch (final Exception e) {
Logger.warnAndDebug(this.getClass(), "isFullScreenField failed: " + ExceptionUtil.getErrorMessage(e) + ", field: " + field, e);
}
return false;
}
}