Skip to content

Commit

Permalink
#21697 adding the create type from base type
Browse files Browse the repository at this point in the history
  • Loading branch information
jdotcms committed Feb 16, 2022
1 parent f80f7af commit c0c35b5
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ public interface ContentTypeAPI {
*/
void moveToSystemFolder(Folder folder) throws DotDataException;

/**
* Saves a new content type based on another existing type
* @param type {@link ContentType}
* @param newVariableName {@link String}
* @return ContentType
* @throws DotDataException Error occurred when performing the action.
* @throws DotSecurityException The user does not have permissions to perform this action.
*/
ContentType saveFrom(ContentType type, String newVariableName) throws DotDataException, DotSecurityException;

/**
* Saves a new Content Type.
*
Expand Down Expand Up @@ -353,4 +363,5 @@ void unlinkPageFromContentType(ContentType contentType)
* @return
*/
boolean isContentTypeAllowed(ContentType contentType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.dotcms.enterprise.license.LicenseLevel;
import com.dotcms.enterprise.license.LicenseManager;
import com.dotcms.exception.BaseRuntimeInternationalizationException;
import com.dotmarketing.portlets.folders.business.FolderAPI;
import com.google.common.collect.ImmutableList;
import com.dotcms.system.event.local.business.LocalSystemEventsAPI;
import com.dotcms.util.ContentTypeUtil;
Expand Down Expand Up @@ -195,7 +196,35 @@ public int count(final String condition, final BaseContentType base, final Strin
}
}

@WrapInTransaction
@Override
public ContentType saveFrom(final ContentType currentType, final String newVariableName) throws DotDataException, DotSecurityException {

final ContentType contentType = ContentTypeBuilder.builder(currentType)
.id(null).modDate(new Date())
.variable(newVariableName)
.build();

final ContentType newContentType = this.save(contentType);

final List<Field> currentFields = currentType.fields();
final List<Field> newFields = new ArrayList<>();

final Map<String, Field> baseFieldMap = newContentType.fieldMap();
for (final Field currentField : currentFields) {

if(!baseFieldMap.containsKey(currentField.variable())) {
newFields.add(FieldBuilder.builder(currentField).contentTypeId(newContentType.id()).id(null).build());
} else {
newFields.add(baseFieldMap.get(currentField.variable()));
}
}


APILocator.getContentTypeFieldAPI().saveFields(newFields, user);

return newContentType;
}

@WrapInTransaction
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,69 @@ public ContentTypeResource(final ContentTypeHelper contentletHelper, final WebRe

public static final String SELECTED_STRUCTURE_KEY = "selectedStructure";

@POST
@Path("/base/{baseVariableName}/new/{newVariableName}")
@JSONP
@NoCache
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON, "application/javascript"})
public final Response createTypeFromBaseType(@Context final HttpServletRequest req,
@Context final HttpServletResponse res,
@PathParam("baseVariableName") final String baseVariableName,
@PathParam("newVariableName") final String newVariableName) {

final InitDataObject initData = this.webResource.init(null, req, res, true, null);
final User user = initData.getUser();
Response response = null;

try {

Logger.debug(this, ()->String.format("Creating new content type '%s' based from '%s' ", baseVariableName, newVariableName));
final HttpSession session = req.getSession(false);

// Validate input
final ContentTypeAPI contentTypeAPI = APILocator.getContentTypeAPI(user, true);
final ContentType type = contentTypeAPI.find(baseVariableName);

if (null == type || (UtilMethods.isSet(type.id()) && !UUIDUtil.isUUID(type.id()))) {

return ExceptionMapperUtil.createResponse(null, "ContentType 'id' if set, should be a uuid");
}

final ContentType contentTypeSaved = contentTypeAPI.saveFrom(type, newVariableName);
final List<SystemActionWorkflowActionMapping> systemActionWorkflowActionMappings = this.workflowHelper.findSystemActionsByContentType(type, user);
final ImmutableMap<Object, Object> responseMap = ImmutableMap.builder()
.putAll(new JsonContentTypeTransformer(contentTypeSaved).mapObject())
.put("workflows", this.workflowHelper.findSchemesByContentType(contentTypeSaved.id(), initData.getUser()))
.put("systemActionMappings", systemActionWorkflowActionMappings.stream()
.collect(Collectors.toMap(mapping-> mapping.getSystemAction(), mapping->mapping)))
.build();

// save the last one to the session to be compliant with #13719
if(null != session) {
session.removeAttribute(SELECTED_STRUCTURE_KEY);
}

response = Response.ok(new ResponseEntityView(responseMap)).build();
} catch (IllegalArgumentException e) {
Logger.error(this, e.getMessage(), e);
response = ExceptionMapperUtil
.createResponse(null, "Content-type is not valid (" + e.getMessage() + ")");
}catch (DotStateException | DotDataException e) {
Logger.error(this, e.getMessage(), e);
response = ExceptionMapperUtil
.createResponse(null, "Content-type is not valid (" + e.getMessage() + ")");
} catch (DotSecurityException e) {
throw new ForbiddenException(e);

} catch (Exception e) {
Logger.error(this, e.getMessage(), e);
response = ExceptionMapperUtil.createResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
}

return response;
}

@POST
@JSONP
@NoCache
Expand Down

0 comments on commit c0c35b5

Please sign in to comment.