Skip to content

Commit

Permalink
Issue 11668 non defualt datatypes (#11671)
Browse files Browse the repository at this point in the history
* #11648 language fallback now works better

* #11668 fixes field saving, file cleanup

* #11668 only delete db if field is in db

* #11668 prevent read-only or fixed fields from being deleted

* #11668 retain fixed/read only properties when editing a field

* #11668 handle null dbType

* #11668 handle null values

* #11668 personas should be a system object

* #11668 starter loading

* #11668 fixing the one failing test

(cherry picked from commit bdd5a31)
  • Loading branch information
wezell authored and jgambarios committed May 22, 2017
1 parent 0ce9ec9 commit c38fc9d
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2298,6 +2298,10 @@ public List<Map<String, String>> getMostViewedContent(String structureInode, Dat
* An error occurred when updating the contents.
*/
protected void clearField(String structureInode, Field field) throws DotDataException {
// we are not a db field;
if(field.getFieldContentlet() == null || ! (field.getFieldContentlet().matches("^.*\\d+$"))){
return;
}
Queries queries = getQueries(field);
List<String> inodesToFlush = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ private ContentType dbSaveUpdate(final ContentType saveType) throws DotDataExcep
}
}

FieldAPI fapi = new FieldAPIImpl().instance();
FieldAPI fapi = APILocator.getContentTypeFieldAPI();
for (Field f : fields) {
f = FieldBuilder.builder(f).contentTypeId(retType.id()).build();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ public void delete(Field field, User user) throws DotDataException, DotSecurityE

perAPI.checkPermission(type, PermissionLevel.PUBLISH, user);

Field oldField = fac.byId(field.id());
if(oldField.fixed() || oldField.readOnly()){
throw new DotDataException("You cannot delete a fixed or read only fiedl");
}

Structure structure = new StructureTransformer(type).asStructure();
com.dotmarketing.portlets.structure.model.Field legacyField = new LegacyFieldTransformer(field).asOldField();
Expand Down Expand Up @@ -193,8 +197,9 @@ public void delete(Field field, User user) throws DotDataException, DotSecurityE
}

// rebuild contentlets indexes
conAPI.reindex(structure);

if(field.indexed()){
conAPI.reindex(structure);
}
// remove the file from the cache
ContentletServices.removeContentletFile(structure);
ContentletMapServices.removeContentletMapFile(structure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,20 @@ private Field dbSaveUpdate(final Field throwAwayField) throws DotDataException {
Field oldField = null;
try {
oldField = selectInDb(throwAwayField.id());
builder.fixed(oldField.fixed());
builder.readOnly(oldField.readOnly());
builder.dataType(oldField.dataType());

} catch (NotFoundInDbException e) {
// this is a new field
}



if (oldField == null) {
// assign a db column if we need to
if (throwAwayField.dbColumn() == null) {
builder.dbColumn(nextAvailableColumn(throwAwayField));
}
// assign an inode if needed

// assign an inode and db column if needed
if (throwAwayField.id() == null) {
builder.id(UUID.randomUUID().toString());
}
if( throwAwayField.dbColumn()==null){
builder.dbColumn(nextAvailableColumn(throwAwayField));
}


if (throwAwayField.sortOrder() < 0) {
// move to the end of the line
Expand All @@ -213,8 +212,6 @@ private Field dbSaveUpdate(final Field throwAwayField) throws DotDataException {
? suggestVelocityVar(throwAwayField.name(), fieldsAlreadyAdded) : throwAwayField.variable();
builder.variable(tryVar);

builder.fixed(false);
builder.readOnly(false);
}


Expand Down Expand Up @@ -255,7 +252,9 @@ private Field dbSaveUpdate(final Field throwAwayField) throws DotDataException {
Field retField = builder.build();



if(retField.dbColumn()==null){
throw new DotDataException("Unable to save field without a DB Column ('field_contentlet')");
}


if (oldField == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.dotcms.contenttype.model.field;


import com.dotmarketing.util.UtilMethods;

public enum DataTypes {
NONE("none"),
Expand All @@ -23,11 +23,13 @@ public String toString () {
return value;
}


public static DataTypes getDataType (String value) {
if (value.isEmpty() || value.contains("_divider") || value.contains("binary") || value.contains("_tab") || value
if (!UtilMethods.isSet(value) || value.contains("_divider") || value.contains("binary") || value.contains("_tab") || value
.contains("constant")) {
return SYSTEM;
}
value = value.replaceAll("[0-9]", "");
DataTypes[] types = DataTypes.values();
for (DataTypes type : types) {
if (type.value.equals(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Class type() {
@Value.Derived
@Override
public List<DataTypes> acceptedDataTypes(){
return ImmutableList.of(DataTypes.LONG_TEXT);
return ImmutableList.of(DataTypes.LONG_TEXT, DataTypes.SYSTEM, DataTypes.TEXT);
}
@Value.Default
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public String defaultValue() {

@Override
public DataTypes dataType() {
String dbType = map.get("field_contentlet").toString().replaceAll("[0-9]", "");
return DataTypes.getDataType(dbType);
return DataTypes.getDataType((String) map.get("field_contentlet"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.elasticsearch.common.Nullable;

import com.dotcms.contenttype.model.field.BinaryField;
import com.dotcms.contenttype.model.field.DataTypes;
import com.dotcms.contenttype.model.field.Field;
import com.dotcms.contenttype.model.field.FieldBuilder;
Expand Down Expand Up @@ -98,10 +99,11 @@ private static com.dotmarketing.portlets.structure.model.Field transformToOld(Fi
}

private static String buildLegacyFieldContent(Field field){
String fieldContent = field.dbColumn();
if(field.typeName().endsWith("BinaryField")){
fieldContent = "binary" + field.sortOrder();
}
String fieldContent = (field instanceof BinaryField)
? fieldContent = "binary" + field.sortOrder()
: (field.dbColumn() !=null)
? field.dbColumn()
: " system_field";
return fieldContent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public void createDefualtPersonaStructure() throws DotDataException {
dc.addParam(PersonaAPI.DEFAULT_PERSONAS_STRUCTURE_NAME);
dc.addParam(PersonaAPI.DEFAULT_PERSONAS_STRUCTURE_DESCRIPTION);
dc.addParam(Structure.STRUCTURE_TYPE_PERSONA);
dc.addParam(false);
dc.addParam(false);
dc.addParam(true);
dc.addParam(true);
dc.addParam(PersonaAPI.DEFAULT_PERSONAS_STRUCTURE_VARNAME);
dc.addParam(Host.SYSTEM_HOST);
dc.addParam(FolderAPI.SYSTEM_FOLDER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.dotcms.contenttype.model.field.FieldBuilder;
import com.dotcms.contenttype.model.field.LegacyFieldTypes;
import com.dotcms.contenttype.transform.field.LegacyFieldTransformer;
import com.dotcms.repackage.com.google.common.annotations.VisibleForTesting;
import com.dotcms.repackage.javax.portlet.ActionRequest;
import com.dotcms.repackage.javax.portlet.ActionResponse;
Expand Down Expand Up @@ -346,43 +348,18 @@ private boolean _saveField(ActionForm form, ActionRequest req, ActionResponse re
field.setSortOrder(sortOrder + 1);
field.setFixed(false);
field.setReadOnly(false);
field.setFieldContentlet(dataType);

String fieldVelocityName = VelocityUtil.convertToVelocityVariable(fieldForm.getFieldName(), false);
int found = 0;
if (VelocityUtil.isNotAllowedVelocityVariableName(fieldVelocityName)) {
found++;
}

String velvar;
for (Field f : fields) {
velvar = f.getVelocityVarName();
if (velvar != null) {
if (fieldVelocityName.equals(velvar)) {
found++;
} else if (velvar.contains(fieldVelocityName)) {
String number = velvar.substring(fieldVelocityName.length());
if (RegEX.contains(number, "^[0-9]+$")) {
found++;
}
}
}
}
if (found > 0) {
fieldVelocityName = fieldVelocityName + Integer.toString(found);
}
}

if(!validateInternalFieldVelocityVarName(fieldVelocityName)){
fieldVelocityName+="1";
}
com.dotcms.contenttype.model.field.Field newField = new LegacyFieldTransformer(field).from();
APILocator.getContentTypeFieldAPI().save(newField, user);

field.setVelocityVarName(fieldVelocityName);
}


boolean isUpdating = UtilMethods.isSet(field.getInode());
// saves this field
FieldFactory.saveField(field);

if(isUpdating) {
if(UtilMethods.isSet(field.getInode())) {
ActivityLogger.logInfo(ActivityLogger.class, "Update Field Action", "User " + _getUser(req).getUserId() + "/" + _getUser(req).getFirstName() + " modified field " + field.getFieldName() + " to " + structure.getName()
+ " Structure.", HostUtil.hostNameUtil(req, _getUser(req)));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dotmarketing.portlets.structure.model;

import java.util.Date;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -12,10 +13,20 @@
import com.dotmarketing.exception.DotHibernateException;
import com.dotmarketing.portlets.structure.factories.FieldFactory;
import com.dotmarketing.util.UtilMethods;

/**
*
* @deprecated use {@link com.dotcms.contenttype.model.field.Field}
*
*/
@Deprecated
public class Field extends Inode implements FieldIf
{

/**
*
* @deprecated
*
*/
@Deprecated
public enum FieldType {

BUTTON("button"),
Expand Down Expand Up @@ -66,6 +77,12 @@ public static FieldType getFieldType (String value) {

}

/**
*
* @deprecated
*
*/
@Deprecated
public enum DataType {

BOOL("bool"),
Expand Down

0 comments on commit c38fc9d

Please sign in to comment.