@@ -240,7 +240,7 @@ public OrderBy getOrderBy() {
240240 return new OrderBy ().add (Entity .OID , getSortType ());
241241 }
242242
243- Optional <SortSpec > requestSortSpec = requestSortKey .map (this ::getSortSpec );
243+ Optional <SortSpec > requestSortSpec = requestSortKey .map (this ::getRequestSortSpec );
244244 Stream <SortSpec > settingSortSpecs = sortSettings .stream ()
245245 .map (this ::getSettingSortSpec );
246246
@@ -251,42 +251,39 @@ public OrderBy getOrderBy() {
251251 return orderBy ;
252252 }
253253
254- //TODO: ロジック共通化
254+ /**
255+ * ソート設定からソート条件を取得します。
256+ */
255257 private SortSpec getSettingSortSpec (SortSetting ss ) {
256258 String sortKey = ss .getSortKey ();
257- PropertyDefinition pd = getPropertyDefinition (sortKey );
258- // ソートキーに参照プロパティ自体が指定された場合(参照先Entityのプロパティまで明示指定された場合は除く)
259- if ( pd instanceof ReferenceProperty ) {
259+ EntityField field = switch ( getPropertyDefinition (sortKey )) {
260+ case ReferenceProperty ref -> {
261+ // ソートキーに参照プロパティ自体が指定された場合(参照先Entityのプロパティまで明示指定された場合は除く)
260262 PropertyColumn property = getLayoutPropertyColumn (sortKey );
261- // 当該項目が画面上表示される場合は、画面上の表示項目でソート
262- if (property != null ) {
263- if (property .getPropertyName ()
264- .equals (sortKey )) {
265- // ソートキーが直接D&Dされた列の場合
266- sortKey = sortKey + "." + getReferencePropertyDisplayName (property .getEditor ());
267- } else {
268- // ネストの存在チェック
269- NestProperty np = getLayoutNestProperty (property , sortKey );
270- if (np != null ) {
271- sortKey = sortKey + "." + getReferencePropertyDisplayName (np .getEditor ());
272- } else {
273- // 画面上に表示されない場合は、Nameでソート
274- sortKey = sortKey + "." + Entity .NAME ;
275- }
276- }
277- } else {
278- // 画面上に表示されない場合は、Nameでソート
279- sortKey = sortKey + "." + Entity .NAME ;
263+ EntityField fallbackField = new EntityField (sortKey + "." + Entity .NAME );
264+
265+ if (property == null ) {
266+ // 画面上に表示されない場合
267+ yield fallbackField ;
280268 }
269+ yield findInSearchResult (sortKey , property , () -> new EntityField (sortKey + "." + getReferencePropertyDisplayName (property .getEditor ())),
270+ (np ) -> new EntityField (sortKey + "." + getReferencePropertyDisplayName (np .getEditor ())))
271+ .orElse (fallbackField );
272+
281273 }
274+ default -> new EntityField (sortKey );
275+ };
276+
282277 SortType type = SortType .valueOf (ss .getSortType ()
283278 .name ());
284279 NullOrderingSpec nullOrderingSpec = getNullOrderingSpec (ss .getNullOrderType ());
285- SortSpec sortSpec = new SortSpec (new EntityField (sortKey ), type , nullOrderingSpec );
286- return sortSpec ;
280+ return new SortSpec (field , type , nullOrderingSpec );
287281 }
288282
289- private SortSpec getSortSpec (String sortKey ) {
283+ /**
284+ * リクエストからソート条件を取得します。
285+ */
286+ private SortSpec getRequestSortSpec (String sortKey ) {
290287 PropertyDefinition pd = getPropertyDefinition (sortKey );
291288 if (pd == null ) {
292289 throw new ApplicationException ("invalid sort key: " + sortKey );
@@ -300,34 +297,33 @@ private SortSpec getSortSpec(String sortKey) {
300297 if (property == null ) {
301298 throw new ApplicationException ("invalid sort key: " + sortKey );
302299 }
303- EntityField field = findEntityFiled (sortKey , pd , property );
304-
305- NullOrderingSpec nullOrderingSpec = getNullOrderingSpec (property .getNullOrderType ());
306- return new SortSpec (field , getSortType (), nullOrderingSpec );
307- }
308-
309- private EntityField findEntityFiled (String sortKey , PropertyDefinition pd , PropertyColumn property ) {
310- // 参照プロパティの場合、画面上の表示項目でソート
311- if (pd instanceof ReferenceProperty ) {
312- return findEntityFiled (sortKey , property , () -> new EntityField (sortKey + "." + getReferencePropertyDisplayName (property .getEditor ())),
313- (np ) -> new EntityField (sortKey + "." + getReferencePropertyDisplayName (np .getEditor ())));
314- }
315- return findEntityFiled (sortKey , property , () -> new EntityField (sortKey ), (np ) -> new EntityField (sortKey ));
300+ Optional <EntityField > field = switch (pd ) {
301+ case ReferenceProperty ref -> findInSearchResult (sortKey , property ,
302+ () -> new EntityField (sortKey + "." + getReferencePropertyDisplayName (property .getEditor ())),
303+ (np ) -> new EntityField (sortKey + "." + getReferencePropertyDisplayName (np .getEditor ())));
304+ default -> findInSearchResult (sortKey , property , () -> new EntityField (sortKey ), (np ) -> new EntityField (sortKey ));
305+ };
306+ return field .map (f -> new SortSpec (f , getSortType (), getNullOrderingSpec (property .getNullOrderType ())))
307+ .orElseThrow (() -> new ApplicationException ("invalid sort key: " + sortKey ));
316308 }
317309
318- private EntityField findEntityFiled (String sortKey , PropertyColumn property , Supplier <EntityField > dndFieldGetter ,
310+ /**
311+ * 検索結果からソートキーに対応するEntityFieldを取得します。
312+ */
313+ private Optional <EntityField > findInSearchResult (String sortKey , PropertyColumn property , Supplier <EntityField > dndFieldGetter ,
319314 Function <NestProperty , EntityField > nestPropFieldGetter ) {
320315 if (property .getPropertyName ()
321316 .equals (sortKey )) {
322317 // ソートキーが直接D&Dされた列の場合
323- return dndFieldGetter .get ();
318+ return Optional . of ( dndFieldGetter .get () );
324319 }
325320 // ネストの存在チェック
326321 NestProperty np = getLayoutNestProperty (property , sortKey );
327322 if (np == null ) {
328- throw new ApplicationException ("invalid sort key: " + sortKey );
323+ // 画面上に表示されない場合
324+ return Optional .empty ();
329325 }
330- return nestPropFieldGetter .apply (np );
326+ return Optional . of ( nestPropFieldGetter .apply (np ) );
331327 }
332328
333329 @ Override
@@ -600,7 +596,7 @@ final List<SortSetting> getSortSetting() {
600596 //画面でソート条件が指定されれば第1キーに
601597 String sortKey = getRequest ().getParam (Constants .SEARCH_SORTKEY );
602598 if (StringUtil .isNotBlank (sortKey )) {
603- setting .add (getRequestSortSpec (sortKey ));
599+ setting .add (_getRequestSortSpec (sortKey ));
604600 }
605601
606602 setting .addAll (getSortSettings ());
@@ -617,7 +613,7 @@ final List<SortSetting> getSortSettings() {
617613 }
618614
619615 //TODO: 削除(共通化)
620- private SortSetting getRequestSortSpec (String sortKey ) {
616+ private SortSetting _getRequestSortSpec (String sortKey ) {
621617 //ロジック変更:ソート設定なし側に合わせる
622618 if (Entity .OID .equals (sortKey )) {
623619 SortSetting ss = new SortSetting ();
0 commit comments