/**
*
* Copyright
* 2009-2015 Jayway Products AB
* 2016-2017 Föreningen Sambruk
*
* Licensed under AGPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/agpl.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package se.streamsource.streamflow.web.domain.structure.form;
import org.qi4j.api.injection.scope.Structure;
import org.qi4j.api.injection.scope.This;
import org.qi4j.api.mixin.Mixins;
import org.qi4j.api.structure.Module;
import se.streamsource.streamflow.api.workspace.cases.contact.ContactBuilder;
import se.streamsource.streamflow.api.workspace.cases.general.FormSignatureDTO;
import se.streamsource.streamflow.api.workspace.cases.general.SecondSigneeInfoValue;
import se.streamsource.streamflow.web.domain.entity.caze.CaseEntity;
import se.streamsource.streamflow.web.domain.entity.gtd.Drafts;
import se.streamsource.streamflow.web.domain.structure.caze.Case;
import se.streamsource.streamflow.web.domain.structure.label.Label;
import se.streamsource.streamflow.web.domain.structure.label.Labelable;
import se.streamsource.streamflow.web.domain.structure.organization.AccessPointSettings;
import se.streamsource.streamflow.web.domain.structure.user.Contactable;
import se.streamsource.streamflow.web.domain.structure.user.EndUser;
import static se.streamsource.streamflow.util.Strings.*;
/**
* JAVADOC
*/
@Mixins(EndUserCases.Mixin.class)
public interface EndUserCases
{
CaseEntity createCaseWithForm( EndUser endUser );
CaseEntity createCase( Drafts endUser );
SubmittedFormValue submitFormAndSendCase( Case caze, FormDraft formSubmission, Submitter submitter );
SubmittedFormValue submitForm( Case caze, FormDraft formSubmission, Submitter submitter );
void sendTo( Case caze );
void discardCase( Case caze );
abstract class Mixin
implements EndUserCases
{
@This
SelectedForms.Data selectedForms;
@This
AccessPointSettings.Data accesspoint;
@This
Labelable.Data labelable;
@Structure
Module module;
public CaseEntity createCaseWithForm( EndUser endUser )
{
CaseEntity caseEntity = createCase( endUser );
caseEntity.createFormDraft( selectedForms.selectedForms().get( 0 ) );
return caseEntity;
}
public SubmittedFormValue submitForm( Case caze, FormDraft formSubmission, Submitter submitter )
{
// Transfer contact information from submitter
// TODO Also add from typed form data
Contactable contactable = (Contactable) caze.createdBy().get();
if (!empty(contactable.getContact().contactId().get()))
caze.addContact(contactable.getContact());
// Add contact info for signatories
for (FormSignatureDTO signature : formSubmission.getFormDraftValue().signatures().get())
{
ContactBuilder builder = new ContactBuilder(module.valueBuilderFactory());
builder.name(signature.signerName().get()).contactId(signature.signerId().get());
caze.addContact(builder.newInstance());
}
if( formSubmission.getFormDraftValue().secondsignee().get() != null
&&!formSubmission.getFormDraftValue().secondsignee().get().singlesignature().get() )
{
SecondSigneeInfoValue secondSignee = formSubmission.getFormDraftValue().secondsignee().get();
ContactBuilder builder = new ContactBuilder( module.valueBuilderFactory() );
builder.name( secondSignee.name().get() );
builder.email( secondSignee.email().get() );
builder.phoneNumber( secondSignee.phonenumber().get() );
builder.contactId( secondSignee.socialsecuritynumber().get() );
caze.addContact( builder.newInstance() );
}
// Submit the form
return caze.submitForm( formSubmission, submitter );
}
public SubmittedFormValue submitFormAndSendCase( Case caze, FormDraft formSubmission, Submitter submitter )
{
SubmittedFormValue submittedForm = submitForm( caze, formSubmission, submitter );
sendTo( caze );
return submittedForm;
}
public CaseEntity createCase( Drafts endUser )
{
CaseEntity caseEntity = endUser.createDraft();
caseEntity.changeCaseType( accesspoint.caseType().get() );
for (Label label : labelable.labels())
{
caseEntity.addLabel( label );
}
return caseEntity;
}
public void sendTo( Case caze )
{
CaseEntity caseEntity = (CaseEntity) caze;
if (caseEntity.isAssigned())
caseEntity.unassign();
caseEntity.changeOwner( accesspoint.project().get() );
caseEntity.open();
}
public void discardCase( Case caze )
{
caze.deleteEntity();
}
}
}