View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.mylasta.direction.sponsor;
17  
18  import java.util.List;
19  import java.util.Locale;
20  
21  import org.codelibs.fess.mylasta.direction.FessConfig;
22  import org.dbflute.mail.send.SMailDeliveryDepartment;
23  import org.dbflute.mail.send.SMailPostalMotorbike;
24  import org.dbflute.mail.send.SMailPostalParkingLot;
25  import org.dbflute.mail.send.SMailPostalPersonnel;
26  import org.dbflute.mail.send.embedded.personnel.SMailDogmaticPostalPersonnel;
27  import org.dbflute.mail.send.embedded.receptionist.SMailConventionReceptionist;
28  import org.dbflute.mail.send.supplement.async.SMailAsyncStrategy;
29  import org.dbflute.mail.send.supplement.filter.SMailSubjectFilter;
30  import org.dbflute.mail.send.supplement.label.SMailLabelStrategy;
31  import org.dbflute.optional.OptionalThing;
32  import org.dbflute.util.DfStringUtil;
33  import org.lastaflute.core.magic.async.AsyncManager;
34  import org.lastaflute.core.magic.async.ConcurrentAsyncCall;
35  import org.lastaflute.core.message.MessageManager;
36  import org.lastaflute.core.util.ContainerUtil;
37  
38  /**
39   * The creator of mail delivery department.
40   *
41   * @author jflute
42   */
43  public class FessMailDeliveryDepartmentCreator {
44  
45      // ===================================================================================
46      //                                                                           Attribute
47      //                                                                           =========
48      protected final FessConfig fessConfig;
49  
50      // ===================================================================================
51      //                                                                         Constructor
52      //                                                                         ===========
53      public FessMailDeliveryDepartmentCreator(final FessConfig fessConfig) {
54          this.fessConfig = fessConfig;
55      }
56  
57      // ===================================================================================
58      //                                                                              Create
59      //                                                                              ======
60      public SMailDeliveryDepartment create() {
61          return new SMailDeliveryDepartment(createPostalParkingLot(), createPostalPersonnel());
62      }
63  
64      // -----------------------------------------------------
65      //                                    Postal Parking Lot
66      //                                    ------------------
67      protected SMailPostalParkingLot createPostalParkingLot() {
68          final SMailPostalParkingLot parkingLot = new SMailPostalParkingLot();
69          final SMailPostalMotorbike motorbike = new SMailPostalMotorbike();
70          final String hostAndPort = fessConfig.getMailSmtpServerMainHostAndPort();
71          final List<String> hostPortList = DfStringUtil.splitListTrimmed(hostAndPort, ":");
72          motorbike.registerConnectionInfo(hostPortList.get(0), Integer.parseInt(hostPortList.get(1)));
73          motorbike.registerReturnPath(fessConfig.getMailReturnPath());
74          parkingLot.registerMotorbikeAsMain(motorbike);
75          return parkingLot;
76      }
77  
78      // -----------------------------------------------------
79      //                                      Postal Personnel
80      //                                      ----------------
81      protected SMailPostalPersonnel createPostalPersonnel() {
82          final SMailDogmaticPostalPersonnel personnel = createDogmaticPostalPersonnel();
83          return fessConfig.isMailSendMock() ? personnel.asTraining() : personnel;
84      }
85  
86      protected SMailDogmaticPostalPersonnel createDogmaticPostalPersonnel() { // #ext_point e.g. locale, database
87          final AsyncManager asyncManager = getAsyncManager();
88          final MessageManager messageManager = getMessageManager();
89          return new SMailDogmaticPostalPersonnel() {
90  
91              // *if you need user locale switching or templates from database,
92              // override createConventionReceptionist() (see the method for the details)
93              @Override
94              protected SMailConventionReceptionist createConventionReceptionist() {
95                  return super.createConventionReceptionist().asReceiverLocale(postcard -> OptionalThing.empty());
96              }
97  
98              @Override
99              protected OptionalThing<SMailSubjectFilter> createSubjectFilter() {
100                 return OptionalThing.of((view, subject) -> subject);
101             }
102 
103             @Override
104             protected OptionalThing<SMailAsyncStrategy> createAsyncStrategy() {
105                 return OptionalThing.of((view, runnable) -> async(asyncManager, runnable));
106             }
107 
108             @Override
109             protected OptionalThing<SMailLabelStrategy> createLabelStrategy() {
110                 return OptionalThing.of((view, locale, label) -> resolveLabelIfNeeds(messageManager, locale, label));
111             }
112         };
113     }
114 
115     // ===================================================================================
116     //                                                                        Asynchronous
117     //                                                                        ============
118     protected void async(final AsyncManager asyncManager, final Runnable runnable) {
119         asyncManager.async(new ConcurrentAsyncCall() {
120             @Override
121             public void callback() {
122                 runnable.run();
123             }
124 
125             @Override
126             public boolean asPrimary() {
127                 return true; // mail is primary business
128             }
129         });
130     }
131 
132     // ===================================================================================
133     //                                                                       Resolve Label
134     //                                                                       =============
135     protected String resolveLabelIfNeeds(final MessageManager messageManager, final Locale locale, final String label) {
136         return label.startsWith("labels.") ? messageManager.getMessage(locale, label) : label;
137     }
138 
139     // ===================================================================================
140     //                                                                           Component
141     //                                                                           =========
142     protected MessageManager getMessageManager() {
143         return ContainerUtil.getComponent(MessageManager.class);
144     }
145 
146     protected AsyncManager getAsyncManager() {
147         return ContainerUtil.getComponent(AsyncManager.class);
148     }
149 }