Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 11497 include field variablest mer #11499

Merged
merged 6 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ public Field byContentTypeAndVar(ContentType type, String fieldVar) throws DotDa

@Override
public Field byContentTypeIdAndVar(String id, String fieldVar) throws DotDataException {
return fac.byContentTypeIdFieldVar(id, fieldVar);
try {
return APILocator.getContentTypeAPI(APILocator.systemUser()).find(id).fieldMap().get(fieldVar);
} catch (Exception e) {
throw new DotDataException(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
import java.util.stream.Stream;

import com.dotcms.contenttype.business.sql.FieldSql;
import com.dotcms.contenttype.exception.DotDataValidationException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.dotcms.contenttype.transform.contenttype;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dotcms.contenttype.model.field.Field;
import com.dotcms.contenttype.model.type.ContentType;
import com.dotcms.contenttype.model.type.ContentTypeBuilder;
import com.dotcms.contenttype.transform.JsonHelper;
import com.dotcms.contenttype.transform.JsonTransformer;
import com.dotcms.contenttype.transform.SerialWrapper;
import com.dotcms.contenttype.transform.field.JsonFieldTransformer;
import com.dotcms.repackage.com.google.common.collect.ImmutableList;
import com.dotmarketing.beans.Host;
Expand Down Expand Up @@ -140,7 +140,27 @@ public JSONArray jsonArray() {
return jarr;
}

public List<Map<String, Object>> mapList() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (ContentType type : asList()) {
list.add(new JsonContentTypeTransformer(type).mapObject());
}
return list;
}

public Map<String, Object> mapObject() {
try {
ContentType type = from();
Map<String, Object> typeMap = mapper.convertValue(type, HashMap.class);
typeMap.put("fields", new JsonFieldTransformer(type.fields()).mapList());
typeMap.remove("acceptedDataTypes");
typeMap.remove("dbColumn");

return typeMap;
} catch (Exception e) {
throw new DotStateException(e);
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ public String typeName() {
}

};

return new ImplClassFieldTransformer(field).from();
Field newField = new ImplClassFieldTransformer(field).from();
//hydrate field variables
newField.fieldVariables();
return newField;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dotcms.contenttype.model.field.Field;
import com.dotcms.contenttype.model.field.FieldVariable;
import com.dotcms.contenttype.model.field.ImmutableFieldVariable;
import com.dotcms.contenttype.transform.JsonHelper;
import com.dotcms.contenttype.transform.JsonTransformer;
import com.dotcms.contenttype.transform.SerialWrapper;
import com.dotcms.repackage.com.google.common.collect.ImmutableList;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.util.json.JSONArray;
import com.dotmarketing.util.json.JSONException;
import com.dotmarketing.util.json.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;

public class JsonFieldTransformer implements FieldTransformer, JsonTransformer {
Expand All @@ -39,8 +36,7 @@ public JsonFieldTransformer(String json) {
JSONObject jo = new JSONObject(json);
if (jo.has("fields")) {
l = fromJsonArray(jo.getJSONArray("fields"));
}
else{
} else {
l.add(fromJsonStr(json));
}
} catch (Exception e) {
Expand Down Expand Up @@ -74,14 +70,12 @@ private List<Field> fromJsonArray(JSONArray jarr)
List<Field> fields = new ArrayList<>();
for (int i = 0; i < jarr.length(); i++) {
JSONObject jo = jarr.getJSONObject(i);

jo.remove("acceptedDataTypes");
Field f = fromJsonStr(jo.toString());
if (jo.has("fieldVariables")) {
String varStr = jo.getJSONArray("fieldVariables").toString();
List<FieldVariable> vars = mapper.readValue(varStr,
mapper.getTypeFactory().constructCollectionType(List.class, ImmutableFieldVariable.class));

f.constructFieldVariables(vars);
}
fields.add(f);
Expand Down Expand Up @@ -114,21 +108,7 @@ public List<Field> asList() throws DotStateException {

@Override
public JSONObject jsonObject() {
try {
JSONObject jo = new JSONObject(mapper.writeValueAsString(from()));
jo.remove("acceptedDataTypes");
//jo.remove("iDate");
jo.remove("dbColumn");


return jo;




} catch (JSONException | JsonProcessingException e) {
throw new DotStateException(e);
}
return new JSONObject(mapObject());
}

@Override
Expand All @@ -142,28 +122,25 @@ public JSONArray jsonArray() {
}

public List<Map<String, Object>> mapList() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (Field field : asList()) {
list.add(new JsonFieldTransformer(field).mapObject());
}
return list;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (Field field : asList()) {
list.add(new JsonFieldTransformer(field).mapObject());
}
return list;
}

public Map<String, Object> mapObject() {
try {
Map<String, Object> map = mapper.readValue(
mapper.writeValueAsString(from()),
new TypeReference<Map<String, String>>(){}
);

map.remove("acceptedDataTypes");
map.remove("iDate");
map.remove("dbColumn");

return map;
} catch (IOException e) {
throw new DotStateException(e);
}
try {
Field f = from();
Map<String, Object> field = mapper.convertValue(f, HashMap.class);
field.put("fieldVariables", new JsonFieldVariableTransformer(f.fieldVariables()).mapList());
field.remove("acceptedDataTypes");
field.remove("dbColumn");

return field;
} catch (Exception e) {
throw new DotStateException(e);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dotcms.contenttype.model.field.Field;
import com.dotcms.contenttype.model.field.FieldVariable;
import com.dotcms.repackage.com.google.common.collect.ImmutableList;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.util.json.JSONArray;
import com.dotmarketing.util.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonFieldVariableTransformer {

private static final ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(
Include.NON_NULL
).configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false
);
private static final ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(Include.NON_NULL)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

final List<FieldVariable> list;

Expand All @@ -33,18 +34,18 @@ public JsonFieldVariableTransformer(List<FieldVariable> list) {
}

public JsonFieldVariableTransformer(String json) {
List<FieldVariable> l = new ArrayList<>();
l.add(fromJsonStr(json));
this.list = ImmutableList.copyOf(l);
List<FieldVariable> l = new ArrayList<>();
l.add(fromJsonStr(json));
this.list = ImmutableList.copyOf(l);
}


private FieldVariable fromJsonStr(String input) throws DotStateException {
try {
return (FieldVariable) mapper.readValue(input, FieldVariable.class);
} catch (Exception e) {
throw new DotStateException(e);
}
try {
return (FieldVariable) mapper.readValue(input, FieldVariable.class);
} catch (Exception e) {
throw new DotStateException(e);
}
}


Expand All @@ -58,28 +59,36 @@ public List<FieldVariable> asList() throws DotStateException {


public List<Map<String, Object>> mapList() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (FieldVariable fieldVariable : asList()) {
list.add(new JsonFieldVariableTransformer(fieldVariable).mapObject());
}
return list;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (FieldVariable fieldVariable : asList()) {
list.add(new JsonFieldVariableTransformer(fieldVariable).mapObject());
}
return list;
}

public Map<String, Object> mapObject() {
try {
Map<String, Object> map = mapper.readValue(
mapper.writeValueAsString(from()),
new TypeReference<Map<String, String>>(){}
);

map.remove("modDate");
map.remove("name");
map.remove("userId");

return map;
} catch (IOException e) {
throw new DotStateException(e);
}

Map<String, Object> map = mapper.convertValue(this.from(), HashMap.class);
map.remove("clazz");
map.remove("modDate");
map.remove("name");
map.remove("userId");

return map;

}


public JSONObject jsonObject() {
return new JSONObject(mapObject());
}

public JSONArray jsonArray() {
JSONArray jarr = new JSONArray();
for (FieldVariable var : asList()) {
jarr.add(new JsonFieldVariableTransformer(var).jsonObject());
}
return jarr;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public final Response saveType(@Context final HttpServletRequest req, final Stri
retTypes.add(APILocator.getContentTypeAPI(user, true).save(type, type.fields()));
}

return Response.ok(new ResponseEntityView(new JsonContentTypeTransformer(retTypes).jsonArray().toString())).build();
return Response.ok(new ResponseEntityView(new JsonContentTypeTransformer(retTypes).mapObject())).build();

}

Expand Down Expand Up @@ -112,7 +112,6 @@ public Response deleteType(@PathParam("id") final String id, @Context final Http
joe.put("deleted", type.id());



Response response = Response.ok(joe.toString()).build();
return response;
}
Expand All @@ -131,7 +130,7 @@ public Response getType(@PathParam("id") final String id, @Context final HttpSer
Response response = Response.status(404).build();
try {
ContentType type = tapi.find(id);
response = Response.ok(new JsonContentTypeTransformer(type).jsonObject().toString()).build();
response = Response.ok(new ResponseEntityView(new JsonContentTypeTransformer(type).mapObject())).build();
} catch (NotFoundInDbException nfdb2) {
// nothing to do here, will throw a 404
}
Expand Down