private long getTemplateFolderIdFromPath(final long groupId, long folderId, final List<String> templateFoldersArray,
            final ServiceContext serviceContext) {
        if (templateFoldersArray.size() > 0) {
            DLFolder folder = null;
            try {
                folder = DLFolderLocalServiceUtil.getFolder(groupId, folderId, templateFoldersArray.get(0));
            } catch (NoSuchFolderException e) {
                if (create_folder) {
                    LOGGER.info("No folder with that name exists, creating one automatically");
                    long folderIdFromCounter = 0;
                    try {
                        folderIdFromCounter = CounterLocalServiceUtil.increment();
                        folder = DLFolderLocalServiceUtil.createDLFolder(folderIdFromCounter);
                        folder = DLFolderLocalServiceUtil.addFolder(serviceContext.getUserId(), groupId, groupId, false, folderId,
                                templateFoldersArray.get(0), "", serviceContext);
                        if (templateFoldersArray.size() == 1) {
                            LOGGER.info("Creating file!");
                            Map<String, Fields> fileFields = new HashMap<String, Fields>();
                            InputStream is = default_template.getInputStream();
                            DLFileEntryLocalServiceUtil.addFileEntry(serviceContext.getUserId(), groupId, groupId, folder.getFolderId(),
                                    "view.ftl", MimeTypesUtil.getContentType(default_template.getFile()), "view.ftl", "", "", 0,
                                    fileFields, default_template.getFile(), is, default_template.getFile().length(), serviceContext);
                        }
                    } catch (SystemException e1) {
                        e1.printStackTrace();
                    } catch (PortalException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                } else {
                    LOGGER.info("No folder with that name exists! Either create the folder manually"
                            + " or set create_folder to true in freemarker.properties to do it automatically.");
                    return FreemarkerConstants.MISSING_FOLDER;
                }
            } catch (PortalException e) {
                e.printStackTrace();
            } catch (SystemException e) {
                e.printStackTrace();
            }

            folderId = folder.getFolderId();
            templateFoldersArray.remove(0);
            return getTemplateFolderIdFromPath(groupId, folderId, templateFoldersArray, serviceContext);
        }
        return folderId;
    }