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

#JC-1373 Add dynamic poll item functionality #40

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 @@ -34,7 +34,7 @@ public class PollItem extends Entity {
/**
* Used only by Hibernate.
*/
protected PollItem() {
public PollItem() {
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,32 @@
*/
package org.jtalks.jcommune.plugin.api.web.dto;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.StringUtils;
import org.jtalks.jcommune.model.entity.Poll;
import org.jtalks.jcommune.model.entity.PollItem;
import org.jtalks.jcommune.model.entity.Post;
import org.jtalks.jcommune.model.entity.Topic;
import org.jtalks.jcommune.plugin.api.web.validation.annotations.BbCodeAwareSize;
import org.jtalks.jcommune.plugin.api.web.validation.annotations.BbCodeNesting;
import org.springframework.util.AutoPopulatingList;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
* DTO for {@link Topic} objects. Used for validation and binding to form.
*
*-
* @author Vitaliy Kravchenko
* @author Max Malakhov
* @author Eugeny Batov
*/
public class TopicDto {

@Valid
private Topic topic;


@BbCodeAwareSize(min = Post.MIN_LENGTH, max = Post.MAX_LENGTH)
@BbCodeNesting
private String bodyText;
Expand All @@ -43,6 +48,14 @@ public class TopicDto {
private String unreadIconUrl;
private String readIconUrl;
private String postUrlPrefix;
/**
* Used for UI only
*/
private List<PollItem> pollItemsValue = new AutoPopulatingList(PollItem.class);
/**
* Contains not empty PollItem
*/
private List<PollItem> pollItems = new ArrayList<>();

/**
* Plain object for topic creation
Expand All @@ -65,10 +78,25 @@ public TopicDto(Topic topic) {
/**
* @return topic that used as dto between controllers and services
*/
@Valid
public Topic getTopic() {
copyPollItem();
return topic;
}

/**
* Copy PollItem from UI collection to Poll. Need that for correct Poll validation.
*/
private void copyPollItem() {
CollectionUtils.select(pollItemsValue, new Predicate() {
@Override
public boolean evaluate(Object object) {
PollItem pollItem = (PollItem) object;
return StringUtils.isNotEmpty(pollItem.getName()) && !pollItems.contains(pollItem);
}
}, pollItems);
}

/**
* Set topic in dto. Used in tests.
*
Expand Down Expand Up @@ -165,4 +193,12 @@ public Topic fillTopic(Topic persistentTopic) {
persistentTopic.setSticked(topic.isSticked());
return topic;
}

/**
* @return poll options in string representation.
*/
public List<PollItem> getPollItemsValue() {
topic.getPoll().setPollItems(pollItems);
return pollItemsValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import org.joda.time.DateTime;
import org.jtalks.jcommune.model.entity.*;
import org.jtalks.jcommune.plugin.api.exceptions.NotFoundException;
import org.jtalks.jcommune.service.nontransactional.LocationService;
import org.jtalks.jcommune.web.dto.EntityToDtoConverter;
import org.jtalks.jcommune.plugin.api.web.dto.PostDto;
import org.jtalks.jcommune.plugin.api.web.dto.TopicDto;
import org.jtalks.jcommune.plugin.api.web.util.BreadcrumbBuilder;
Expand All @@ -28,6 +26,7 @@
import org.jtalks.jcommune.web.dto.json.JsonResponse;
import org.jtalks.jcommune.web.dto.json.JsonResponseStatus;
import org.jtalks.jcommune.web.validation.editors.DateTimeEditor;
import org.jtalks.jcommune.web.view.PollItemsConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -70,6 +69,7 @@ public class TopicController {
public static final String POST_DTO = "postDto";
private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
public static final String POLL = "poll";
public static final String POLL_CONFIG = "pollConfig";

private TopicModificationService topicModificationService;
private TopicFetchService topicFetchService;
Expand All @@ -81,6 +81,7 @@ public class TopicController {
private LocationService locationService;
private SessionRegistry sessionRegistry;
private EntityToDtoConverter converter;
private PollItemsConfig pollItemsConfig = new PollItemsConfig();

/**
* This method turns the trim binder on. Trim binder
Expand Down Expand Up @@ -150,6 +151,7 @@ public ModelAndView showNewTopicPage(@RequestParam(BRANCH_ID) Long branchId) thr
return new ModelAndView(TOPIC_VIEW)
.addObject(TOPIC_DTO, dto)
.addObject(BRANCH_ID, branchId)
.addObject(POLL_CONFIG, pollItemsConfig)
.addObject(SUBMIT_URL, "/topics/new?branchId=" + branchId)
.addObject(BREADCRUMB_LIST, breadcrumbBuilder.getNewTopicBreadcrumb(branch));
}
Expand All @@ -172,6 +174,7 @@ public ModelAndView createTopic(@Valid @ModelAttribute TopicDto topicDto,
return new ModelAndView(TOPIC_VIEW)
.addObject(BRANCH_ID, branchId)
.addObject(TOPIC_DTO, topicDto)
.addObject(POLL_CONFIG, pollItemsConfig)
.addObject(SUBMIT_URL, "/topics/new?branchId=" + branchId)
.addObject(BREADCRUMB_LIST, breadcrumbBuilder.getForumBreadcrumb(branch));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (C) 2011 JTalks.org Team
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.jtalks.jcommune.web.view;

import org.jtalks.jcommune.model.entity.Poll;

/**
* Contains configuration for Dynamic Poll Items
* @author Andrey Ivanov
*/
public class PollItemsConfig {

public Integer getMaxPollItems(){
return Poll.MAX_ITEMS_NUMBER;
}

public Integer getMinPollItems(){
return Poll.MIN_ITEMS_NUMBER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ label.topic.notify_message=Notify me about the answer
label.topic.no_smiles=Don't display smiles
label.topic.close=Close
label.topic.open=Reopen
label.addPoll=Add Poll
label.removePoll=Remove Poll
#subscription
label.subscribe=Subscribe
label.subscribe.tooltip=Get mail notifications
Expand Down Expand Up @@ -306,12 +308,13 @@ label.search.header.topic=Topic
#new poll page
label.poll.header=New Poll
label.poll.title=Poll
label.poll.options.title=Option list
label.poll.options.title=Options
label.poll.single.title=Single answer
label.poll.multiple.title=Multiple answers are allowed
label.poll.date=Ending date
label.poll.date.set=Click to set date
label.poll.current_end_date=Current end date
label.poll.options.addPollItem=Add new poll item
#poll
label.poll.vote=Vote
label.poll.option.vote.info= {0} - {1}%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ label.search.header.topic=Tema
#new poll page
label.poll.header=Nueva Encuesta
label.poll.title=Encuesta
label.poll.options.title=Lista de Opciones
label.poll.options.title=Opciones
label.poll.single.title=Respuesta simple
label.poll.multiple.title=La respuesta m\u00FAltiple est\u00E1 permitida
label.poll.date=Fecha de fin
Expand Down Expand Up @@ -462,3 +462,6 @@ label.branch.header.lastMessage.tooltip=Ver \u00FAltimo mensaje
label.topic.section.in=en
label.tips.close=Cerrar este tema
label.tips.open=Reabrir este tema
label.poll.options.addPollItem=Añadir encuesta artículo
label.addPoll=Adicionar enquete
label.removePoll=Remover Poll
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ label.errors.not_empty = \u041D\u0435 \u043C\u043E\u0436\u0435\u0442 \u0431\u044

label.poll.header=\u041D\u043E\u0432\u043E\u0435 \u0433\u043E\u043B\u043E\u0441\u043E\u0432\u0430\u043D\u0438\u0435
label.poll.title=\u0413\u043E\u043B\u043E\u0441\u043E\u0432\u0430\u043D\u0438\u0435
label.poll.options.title=\u0421\u043F\u0438\u0441\u043E\u043A \u0432\u0430\u0440\u0438\u0430\u043D\u0442\u043E\u0432
label.poll.options.title=\u0412\u0430\u0440\u0438\u0430\u043d\u0442
label.poll.single.title=\u041E\u0434\u0438\u043D \u0432\u0430\u0440\u0438\u0430\u043D\u0442
label.poll.multiple.title=\u0412\u043E\u0437\u043C\u043E\u0436\u043D\u043E \u043D\u0435\u0441\u043A\u043E\u043B\u044C\u043A\u043E \u0432\u0430\u0440\u0438\u0430\u043D\u0442\u043E\u0432 \u043E\u0442\u0432\u0435\u0442\u0430
label.poll.date=\u0414\u0430\u0442\u0430 \u043E\u043A\u043E\u043D\u0447\u0430\u043D\u0438\u044F
Expand Down Expand Up @@ -460,3 +460,6 @@ label.branch.header.lastMessage.tooltip=\u041F\u0435\u0440\u0435\u0439\u0442\u04
label.topic.section.in=\u0432
label.tips.close=\u0417\u0430\u043A\u0440\u044B\u0442\u044C \u0442\u0435\u043C\u0443
label.tips.open=\u041E\u0442\u043A\u0440\u044B\u0442\u044C \u0442\u0435\u043C\u0443
label.poll.options.addPollItem=\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442
label.addPoll=\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0435
label.removePoll=\u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0441\u043e\u0432\u0430\u043d\u0438\u0435
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ label.search.header.topic=\u0422\u0435\u043C\u0430
#new poll page
label.poll.header=\u041D\u043E\u0432\u0435 \u043E\u043F\u0438\u0442\u0443\u0432\u0430\u043D\u043D\u044F
label.poll.title=\u041E\u043F\u0438\u0442\u0443\u0432\u0430\u043D\u043D\u044F
label.poll.options.title=\u0421\u043F\u0438\u0441\u043E\u043A \u0432\u0430\u0440\u0456\u0430\u043D\u0442\u0456\u0432
label.poll.options.title=\u0412\u0430\u0440\u0456\u0430\u043d\u0442
label.poll.single.title=\u041E\u0434\u0438\u043D\u043E\u0447\u043D\u0430 \u0432\u0456\u0434\u043F\u043E\u0432\u0456\u0434\u044C
label.poll.multiple.title=\u0414\u043E\u0437\u0432\u043E\u043B\u0435\u043D\u043E \u0432\u0438\u0431\u0438\u0440\u0430\u0442\u0438 \u043A\u0456\u043B\u044C\u043A\u0430 \u0432\u0430\u0440\u0456\u0430\u043D\u0442\u0456\u0432
label.poll.date=\u0414\u0430\u0442\u0430 \u0437\u0430\u043A\u0456\u043D\u0447\u0435\u043D\u043D\u044F
Expand Down Expand Up @@ -462,3 +462,6 @@ label.branch.header.lastMessage.tooltip=\u041F\u0435\u0440\u0435\u0439\u0442\u04
label.topic.section.in=\u0432
label.tips.close=\u0417\u0430\u043A\u0440\u0438\u0442\u0438 \u0442\u0435\u043C\u0443
label.tips.open=\u0412\u0456\u0434\u043A\u0440\u0438\u0442\u0438 \u0442\u0435\u043C\u0443
label.poll.options.addPollItem=\u0414\u043e\u0434\u0430\u0442\u0438 \u043d\u043e\u0432\u0438\u0439 \u0432\u0430\u0440\u0456\u0430\u043d\u0442
label.addPoll=\u0414\u043e\u0434\u0430\u0442\u0438 \u043e\u043f\u0438\u0442\u0443\u0432\u0430\u043d\u043d\u044f
label.removePoll=\u0412\u044b\u0434\u0430\u043b\u0438\u0442\u0438 \u043e\u043f\u0438\u0442\u0443\u0432\u0430\u043d\u043d\u044f
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,15 @@
postText="${topicDto.bodyText}"
bodyParameterName="bodyText"
back="${pageContext.request.contextPath}/branches/${branchId}"/>
<c:if test="${topicId eq null}">
<input id="pollButton" type="button" rel="add" class="btn btn-success space-left-medium-nf" name="pollButton"
tabindex="300" value="<spring:message code="label.addPoll"/>"/>
</c:if>
<c:if test="${topicId eq null || topicDto.poll != null}">
<div class="hide-on-preview">
<br/>
<br/>
<c:if test="${topicId eq null || topicDto.poll != null}">
<div class='well hide-on-preview'>
<div id="editPollHolder" class='well <c:if test="${topicId eq null}">hide</c:if>'>
<fieldset id="editPoll">
<legend>
<c:choose>
Expand All @@ -97,44 +102,80 @@
<spring:message code='label.poll.title' var='pollTitlePlaceholder'/>
<form:input path="topic.poll.title" tabindex="600" type="text" id="pollTitle"
size="45" maxlength="255" placeholder="${pollTitlePlaceholder}"
class="post script-confirm-unsaved" disabled="${pollEditing}"/>
class="post script-confirm-unsaved input-xlarge" disabled="${pollEditing}"/>
<br>
<form:errors path="topic.poll.title" cssClass="help-inline focusToError"/>
</div>
<div class='control-group'>
<spring:message code='label.poll.options.title' var='optionsPlaceholder'/>
<form:textarea path="topic.poll.pollItemsValue" tabindex="700" rows="8" id="pollItems"
class="post script-confirm-unsaved" placeholder="${optionsPlaceholder}"
disabled="${pollEditing}"/>
<br>
<form:errors path="topic.poll.pollItems" cssClass="help-inline focusToError"/>
<spring:message code='label.poll.options.addPollItem' var='addPollItemLabel'/>
<div class="left-aligned">
<c:choose>
<c:when test="${pollEditing}">
<c:forEach items="${topicDto.poll.pollItems}" varStatus="i">
<div class="pollItemsValue">
<form:input path="poll.pollItems[${i.index}]" placeholder="${optionsPlaceholder}"
class="input-medium" disabled="${pollEditing}"/></div>
</c:forEach>
</c:when>
<c:otherwise>
<script type="text/javascript">
var minPollItems = ${pollConfig.minPollItems};
var maxPollItems = ${pollConfig.maxPollItems};
</script>
<div id="sortable" rel="pollItemsValue[__index__]">
<c:choose>
<c:when test="${not empty topicDto.pollItemsValue}">
<c:set value="${topicDto.pollItemsValue.size()-1}" var="pollItemsUpperBound" />
</c:when>
<c:otherwise>
<c:set value="${pollConfig.minPollItems-1}" var="pollItemsUpperBound" />
</c:otherwise>
</c:choose>
<c:forEach begin="0" end="${pollItemsUpperBound}" varStatus="i">
<div class="pollItemsValue">
<div class="input-append">
<form:input path="pollItemsValue[${i.index}]" placeholder="${optionsPlaceholder}"
class="input-xlarge" disabled="${pollEditing}"/>
<a href="#" class="icon-move poll-icon-pull"></a>
<a href="#" class="icon-remove remove poll-icon-pull"></a>
</div>
</div>
</c:forEach>
<a href="#" id="add" class="btn btn-primary btn-xs">${addPollItemLabel}</a>
</div>
<form:errors path="topic.poll.pollItems" cssClass="help-inline focusToError"/>
</c:otherwise>
</c:choose>
</div>
<div class="right-aligned">
<spring:message code="label.poll.date"/>
<spring:message code='label.poll.date.set' var='datePlaceholder'/>
<form:input path="poll.endingDate" tabindex="900" id="datepicker" type="text"
readonly="true" placeholder="${datePlaceholder}"
class="cursor-pointer script-confirm-unsaved space-left-small"/>
<c:if test="${topicId eq null}">
&nbsp;<i class="icon-trash cursor-pointer" id="deleteEndingDate"></i>
</c:if>
<br>
<form:errors path="poll.endingDate" cssClass="help-inline"/>
</div>
</div>

<div class="cleared"></div>
<div class='control-group'>
<form:checkbox path="topic.poll.multipleAnswer" id="multipleChecker"
<br/>
<form:checkbox path="poll.multipleAnswer" id="multipleChecker"
class="form-check-radio-box script-confirm-unsaved"
tabindex="800" value="${topicDto.poll.multipleAnswer}" disabled="${pollEditing}"/>
tabindex="800" value="${poll.multipleAnswer}" disabled="${pollEditing}"/>
<label for='multipleChecker' class='string optional'>
<spring:message code="label.poll.multiple.title"/>
</label>
</div>

<div class="control-group right-aligned">
<spring:message code="label.poll.date"/>
<spring:message code='label.poll.date.set' var='datePlaceholder'/>
<form:input path="topic.poll.endingDate" tabindex="900" id="datepicker" type="text"
readonly="true" placeholder="${datePlaceholder}"
class="cursor-pointer script-confirm-unsaved space-left-small"/>
<c:if test="${topicId eq null}">
&nbsp;<i class="icon-trash cursor-pointer" id="deleteEndingDate"></i>
</c:if>
<br>
<form:errors path="topic.poll.endingDate" cssClass="help-inline"/>
</div>
<%--Make parent div include floated divs explicitly, or they'll be shown out of parent container--%>
<%--Make parent div include floated divs explicitly, or they'll be shown out of parent container--%>
<div class="cleared"></div>
</fieldset>
</div>
</div>
</c:if>
</form:form>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@
</div>

<c:if test="${showSubmitButton}">
<input id="post" type="submit" class="btn btn-primary" accesskey="s" name="post" tabindex="300"
value="<spring:message code="${labelForAction}"/>"/>
<input id="post" type="submit" class="btn btn-primary" accesskey="s" name="post" tabindex="300"
value="<spring:message code="${labelForAction}"/>"/>
</c:if>

<input id="preview" type="button" class="btn btn-success space-left-medium-nf" name="preview" tabindex="400"
value="<spring:message code="label.answer.preview"/>"
onclick="togglePreviewMode(new Array('posts', 'topics'));return null;"/>

<script type="text/javascript">
initEditor("postBody", "editorBBCODEdiv", "htmlContent");
</script>
Loading